如何将PHP / MySQL查询的搜索结果链接到单行数据?
I've been stuck on this problem for a while, but essentially I am trying to make a link from an echo output
echo "<li>" . "<a href=\"testdisplay.php?id=$ID\">" . $RecipeName . "</a></li>
";
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).
我一直坚持这个问题,但基本上我试图从回声中建立一个链接 输出 p>
转到显示或在同一页面中显示所有数据的新页面 在同一个RecipeName的行中(我想显示大约5个字段/列的数据)。 p>
我不确定是否需要创建一个新的php页面 它调用一个可以显示这些字段的方法,或者我可以在同一页面上通过其他方式自动显示它们(即,testdisplay页面 - 与其余代码所在的文件相同)。 / p>
div> echo“&lt; li&gt;” 。 “&lt; a href = \”testdisplay.php?id = $ ID \“&gt;” 。 $ RecipeName。 “&lt; / a&gt;&lt; / li&gt;
”; code> p>
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.
even nicer
$id = (int) $_GET['id'];