【问题标题】:Unable to use a variable in mysql Select无法在 mysql Select 中使用变量
【发布时间】:2010-12-28 21:21:41
【问题描述】:
$select = " SELECT * FROM comments WHERE type=$row->title; ORDER BY id desc" .
         " LIMIT $low, $PerPage";
$final = mysql_query($select) or die('Error!');

$row->标题;是先前创建的,它具有 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!');

【问题讨论】:

    标签: mysql variables select


    【解决方案1】:

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

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

    【讨论】:

    • 添加了引号。 Thx dqhendricks 差点忘了 :)
    【解决方案2】:

    你应该回显 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";
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 2020-08-17
      • 1970-01-01
      相关资源
      最近更新 更多