Hi, How can we do the following,
select * from tblCustomer where Cust_id in (array of id's).
I need to use an array in where condition.
Any ideas on how to do this?
Thanks... Rahul.
Numeric ID select * from tblCustomer where Cust_id in ( join(',',$aryIDs).
Text ID select * from tblCustomer where Cust_id in (' join("','",$aryIDs) ').
No, but you can make that array into a string and then stick it in the SQL query...
<?php foreach ($array as $value) { $string = $string . ", " . $value } $string = substr ($string, 2); mysql_query ("SELECT * FROM tblCustomer WHERE cust_ID IN ( " . $string . " ) "); ?>
Hi superwormy,
That worked. Yes, I could do that way, I knew that we use 'in' where condition. Yea, my brain is acting lazy these days 🙂,
Im new to php/mysql and I thought there is some way where directly we can use array in where condition.
kewl.
You could also use the same method to build something like this:
WHERE IDX = 1 OR IDX = 5 OR IDX = 16 OR IDX = 17
Note also that these results WILL MOST LIKELY NOT be returned in the order you put those numbers in, ie not ordered at all UNLESS you use an ORDER BY claus somewhere else or a fancy CASE statement to order them correctly.
Yep. you are right. I m using a order by in my query, so that works fine.
Thanx.