How do I make search results for a PHP / MySQL query link to data in a single row?
I've been stuck on this problem for a while, but essentially I am trying to make a link from开发者_StackOverflow中文版 an echo output
echo "<li>" . "<a href=\"testdisplay.php?id=$ID\">" . $RecipeName . "</a></li>\n";
go to a new page that displays, or displays in the same page, all the data that is in the row of the same RecipeName (there are about 5 fields / columns of data that I would like to display).
I'm not sure if I need to create a new php page which calls a method that can display these fields, or if I can just display them automatically through some other way on the same page (that is, the testdisplay page - which is the same file that the rest of the code is in).
even nicer
$id = (int) $_GET['id'];
Assuming you're on the page that you want to display the data, you could do this:
<?php
$id = mysql_real_escape_string($_GET['id']); //Get the id that was passed in the URL string and escape it to prevent injection.
$sql = "SELECT * FROM table_name where id = $id";
$res = mysql_query($sql); //Fetch the results from the database (assuming already connected)
$res = mysql_fetch_assoc($res); //converts the result pointer into an associative array.
?>
At this point, $res['column_name']
is the way you'd access the different columns.
You'd also want to look out for empty results, as well.
精彩评论