Forum Moderators: coopster

Message Too Old, No Replies

MySQL query where = an array

Is there anyway to match an arry in a mysql query

         

zootreeves

2:28 pm on Jun 27, 2004 (gmt 0)

10+ Year Member



Hi,

this is quite difficult to explain, but is there any way to use WHERE = array in php, e.g.

$array[] = 23;
$array[] = 334;
$array[] = 20;
$array[] = 64;

$sql = "SELECT FROM Website_table WHERE ID=$array";

Will this work? e.g. the above query should return rows where ID = 23 or 334 or 20.

What about?
$sql = "SELECT FROM Website_table WHERE ID=$array[o] OR ID=$array[1] OR ID=$array[2]";

I can't find any other way to do this because i am already using a while loop to generate the array and then i want to match everything in the array but i cannot use another wile loop! This probably doesn't make any sense..

carneddau

2:45 pm on Jun 27, 2004 (gmt 0)

10+ Year Member



Hi,

Try using

"SELECT * FROM Website_table WHERE ID IN (23, 334, 20, 64)";

You can generate the string of IDs instead of/or whilst creating your array.

Zipper

2:46 pm on Jun 27, 2004 (gmt 0)

10+ Year Member



yes, you may use OR, or IN to give each value as the WHERE clause should be followed by an conditional expression.

and then i want to match everything in the array but i cannot use another wile loop! This probably doesn't make any sense..

in this case, yes. however, imagine there were like 100 id's to lookup. unless you got a direct relationship to the numbers you would have to go while looping ;)