Page 1 of 1

query a bunch of values at the same time

Posted: Fri Jul 06, 2007 5:40 pm
by catstevens
Hi,

I'm pretty new to php but I can't find example code for this anywhere - basically, i need a big textbox where I can paste in a bunch of product ID's, hit submit, and query the database for all the product ID's i just entered. The result set might include product ID and name for example.

I know how to do this with a little one-line textbox for a single product ID, but how should i go about querying a bunch of product IDs at the same time?

Posted: Fri Jul 06, 2007 6:14 pm
by stereofrog
hi there

use sql IN operator:

Code: Select all

select whatever from products where id IN (1, 2, 500, 1239)

Posted: Fri Jul 06, 2007 10:28 pm
by califdon
Yes, the IN operator could be useful for this, but first you have to separate the items so you can put them into the IN parameters. If they are space delimited, you could use explode() to create an array, then load the array into the IN parameters.

Posted: Mon Jul 09, 2007 10:33 am
by catstevens
cool, thanks guys! I'll give these a try

Posted: Mon Jul 09, 2007 10:46 am
by catstevens
one other question - say the input is seperated by line breaks. would i need to convert the line breaks to commas first?

Posted: Mon Jul 09, 2007 1:45 pm
by Begby
catstevens wrote:one other question - say the input is seperated by line breaks. would i need to convert the line breaks to commas first?
Yes. There are a lot of functions in php that can do that pretty easily. You will also need to surround the individual values in quotes if they are text.