无法在 mysql Select 中使用变量

发布于 2024-10-09 15:45:27 字数 506 浏览 1 评论 0原文

$select = " SELECT * FROM comments WHERE type=$row->title; ORDER BY id desc" .
         " LIMIT $low, $PerPage";
$final = mysql_query($select) or die('Error!');

$行->标题;是之前创建的,并且具有类似 Type1、Type2 或其他值的值。当我启动该脚本时,结果是“错误!”。你能告诉我为什么吗?我尝试了很多方法来解决这个问题,但没有任何结果。这是其中之一:

$mytype=$row->title;
$select = " SELECT * FROM comments WHERE type=$mytype; ORDER BY id desc" .
         " LIMIT $low, $PerPage";
$final = mysql_query($select) or die('Error!');
$select = " SELECT * FROM comments WHERE type=$row->title; ORDER BY id desc" .
         " LIMIT $low, $PerPage";
$final = mysql_query($select) or die('Error!');

$row->title; is previously created and it have value like Type1, Type2 or something else. When I start that script the result is "Error!". Could you tell me why? I have tried many ways to reslove the problem but without any result. This is one of them:

$mytype=$row->title;
$select = " SELECT * FROM comments WHERE type=$mytype; ORDER BY id desc" .
         " LIMIT $low, $PerPage";
$final = mysql_query($select) or die('Error!');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵山崖 2024-10-16 15:45:27

删除 ;在 $row-title 之后即:

$select = " SELECT * FROM comments WHERE type='$row->title'  ORDER BY id desc" .          " LIMIT $low, $PerPage"; 

Remove the ; after the $row-title i.e.:

$select = " SELECT * FROM comments WHERE type='$row->title'  ORDER BY id desc" .          " LIMIT $low, $PerPage"; 
-残月青衣踏尘吟 2024-10-16 15:45:27

你应该回显 mysql_error() - MySQL 会告诉你出了什么问题!

dqhendricks 和 Cyber​​nate 的答案是正确的 - 您应该将字符串括在单引号中。

但是 - 您还应该转义您的文本,否则您最终会收到更多错误:

$select = "SELECT * FROM comments WHERE type='" . mysql_real_escape_string($row->title) . "' ORDER BY id desc LIMIT $low, $PerPage";

You should echo out mysql_error() - MySQL will tell you what went wrong!

The answers by dqhendricks and Cybernate are correct - you should enclose your strings in single quotes.

However - you should also be escaping your text, or you will get more errors eventually:

$select = "SELECT * FROM comments WHERE type='" . mysql_real_escape_string($row->title) . "' ORDER BY id desc LIMIT $low, $PerPage";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文