1

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!

3
  • have you tried already to use this SQL directly in phpMyAdmin or somewhere else? Does it work with WHERE color_base1='#92462f' (change the color value to a value that is in the DB)? Does it work without the WHERE-part of the clause? Commented Aug 3, 2012 at 12:11
  • Where is the code that prints "Query was empty"? Commented Aug 3, 2012 at 12:13
  • @ lars - Yes, it works directly in myAdmin. Its not a matter of the code, since the color is a VARCHAR... Any other suggestions? I don't think the syntax is bad, since it works when I don't send the jquery Commented Aug 3, 2012 at 15:55

2 Answers 2

1

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.

0
0

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

1
  • I have tried all that but even when I just SELECT * without any WHERE, it doesn't work... Maybe it's because I am reloading a DIV? Commented Aug 3, 2012 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.