[英]Display only queried ID+row PHP/MySQL
I have my data stored in a MySQL table, which includes an auto_increment ID number (unique) for each new row. 我将数据存储在MySQL表中,其中每个新行都包含一个auto_increment ID号(唯一)。
I'd like users to be able to get a certain ID number, using the $_GET
function. 我希望用户能够使用
$_GET
函数获得特定的ID号。
eg. 例如。 User loads http://mysite.com/id.php?id=123
用户加载http://mysite.com/id.php?id=123
Page displays ID number 123
along with the row. 页面显示ID号
123
和该行。
echo $row['id'];
echo "<table>";
echo "<tr> <th>Unit</th> <th>Message</th> <th>Date</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['title'];
echo "</td><td>";
echo $row['description'];
echo "</td><td>";
echo $row['pubDate'];
echo "</td></tr>";
}
echo "</table>";
echo "</center>";
I'm stuck as to where I put the $_GET
bit. 我对
$_GET
放置位置感到$_GET
。
Thanks :) 谢谢 :)
You should append it to your query (using intval
to avoid SQL injection) like this: 您应该将其附加到查询中(使用
intval
以避免SQL注入),如下所示:
// use the id in your WHERE clause, convert it to an integer to avoid sql injections
$query = 'SELECT fields FROM table WHERE id = ' . intval($_GET['id']);
$result = mysql_query($query);
$row = mysql_fetch_row($result);
... do stuff with $row ...
Firstly, your code does not make much sense, since you use $row
before it was defined. 首先,您的代码没有多大意义,因为您在定义
$row
之前使用了$row
。
Secondly, $result
isn't defined at all, and it should be, for example like this: 其次,根本没有定义
$result
,而是应该这样定义:
$id = intval($_GET['id']);
$result = mysql_query("SELECT FROM table WHERE id = '$id'");
And now you know how and where to use $_GET['id']
. 现在您知道了如何以及在哪里使用
$_GET['id']
。
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `Table` WHERE `id`='" . $id . "'";
$res = mysql_query ($query);
$exist = mysql_num_rows($res);
if ($exist) {
$row = mysqlfetch_assoc($res);
...
}
Dont waste your time doing the comparison afterwards, you'll save yourself alot of time by adding it to the original query 之后不要浪费时间进行比较,将其添加到原始查询中可以节省大量时间
$id = intval($_GET['id']);
$query = "SELECT whatever FROM table WHERE id=$id";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.