samyb8
samyb8

Reputation: 2588

Query was empty mysql php variable from jquery

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

Answers (2)

Jocelyn
Jocelyn

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

asprin
asprin

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

Related Questions