Page 1 of 1

MySQL WHERE IN (*) - possible?

Posted: Thu Mar 19, 2009 5:07 am
by mattpointblank
Hey everyone.

I'm writing an AJAX filtering system. You can select options from checkboxes and my php script returns data relating to the options, dynamically.

I want to add a 'select all' option to the checkbox group. The way it works currently is that when the user clicks a checkbox, its ID number is added to an array, which is then turned into a comma separated string and sent to my script, where I use it like:

SELECT * FROM table WHERE fieldID IN ($string)

I tried giving my 'select all' box a value of * but doing something like:

WHERE IN (2, 3, *) or even WHERE IN (*) doesn't work - any ideas on getting this to work?

Matt

Re: MySQL WHERE IN (*) - possible?

Posted: Thu Mar 19, 2009 5:18 am
by Kadanis
I don't believe "WHERE IN (*)" works at all.

I recommend that you put a small If block in your PHP. Something like

Code: Select all

 
$sql = "SELECT * FROM table";
if (!$selectAll){
   $sql .= " WHERE fieldID IN (1,2,3)";
}
 
//execute $sql in your chosen db method
 
This way if the select all is ticked you have no criteria for the query and the search returns all results. If you have anything else then it adds the WHERE IN clause to the SQL statement before execution.

Re: MySQL WHERE IN (*) - possible?

Posted: Thu Mar 19, 2009 5:28 am
by mattpointblank
Fair enough - just wondered if there was a simpler way. I used your method - cheers.