Reputation: 2588
I have a javascript that loads the contents of a DIV when checking a Checkbox and passes a variable.
I retrieve the variable with $color = $_GET['color'];
and then I do a few IFs to pick my query:
if ($color != '')
{
if ($sortBy != '')
{
$items = mysql_query("SELECT * FROM item_descr WHERE color_base1 = '$color' ORDER BY '$sortBy' DESC");
}
else
{
$items = mysql_query("SELECT * FROM item_descr WHERE color_base1 = $color");
echo $color;
$result = mysql_query($items) or die(mysql_error());
}
}
Every time the $result is returning "Quert was empty" even if $color contains a value.
Note: I have tried to put $color in the query like this too: '$color' and also '".$color."'. Didn´t work Would you have any idea of what's going on?
Thanks!
Upvotes: 1
Views: 377
Reputation: 11393
Try this simpler code:
if ($sortBy != '')
$query = "SELECT * FROM item_descr WHERE color_base1 = '$color' ORDER BY $sortBy DESC";
else
$query = "SELECT * FROM item_descr WHERE color_base1 = '$color'";
$result = mysql_query($query) or die(mysql_error());
I removed the quotes around $sortBy
, I added quotes around $color
.
Upvotes: 1
Reputation: 9823
Variables should be quoted '
or "
$items = mysql_query("SELECT * FROM item_descr WHERE color_base1 = '$color'");
or for better readability
$items = mysql_query("SELECT * FROM item_descr WHERE color_base1 = ".$color);
and remove $result
as $items
will return true or false
Upvotes: 0