开发者

Unable to use a variable in mysql Select

$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开发者_如何学Python die('Error!');


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

$select = " SELECT * FROM comments WHERE type='$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";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜