MySQL WHERE IN (*) - possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

MySQL WHERE IN (*) - possible?

Post 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
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: MySQL WHERE IN (*) - possible?

Post 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.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: MySQL WHERE IN (*) - possible?

Post by mattpointblank »

Fair enough - just wondered if there was a simpler way. I used your method - cheers.
Post Reply