Tran Gonrah
Tran Gonrah

Reputation: 1

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 die('Error!');

Upvotes: 0

Views: 342

Answers (2)

TehShrike
TehShrike

Reputation: 10074

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";

Upvotes: 0

Chandu
Chandu

Reputation: 82903

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

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

Upvotes: 3

Related Questions