March 24, 2012

Select where in array

Question by Jordy

How can I select all records where the id is in the array?

So if I have an array with the values 1,25,2,12,1859 and 192: all the records with the id’s should be returned.

Now I have this:

$query="SELECT * FROM table WHERE Id IN(".implode(",",$my_array).")";

But that doesn’t work. It only returns one value.

Answer by Starx

Add the quotes in the query

$query="SELECT * FROM table WHERE Id IN('".implode("','",$my_array)."')";

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!