query a bunch of values at the same time

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
catstevens
Forum Newbie
Posts: 3
Joined: Fri Jul 06, 2007 5:30 pm

query a bunch of values at the same time

Post 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?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

hi there

use sql IN operator:

Code: Select all

select whatever from products where id IN (1, 2, 500, 1239)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post 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.
catstevens
Forum Newbie
Posts: 3
Joined: Fri Jul 06, 2007 5:30 pm

Post by catstevens »

cool, thanks guys! I'll give these a try
catstevens
Forum Newbie
Posts: 3
Joined: Fri Jul 06, 2007 5:30 pm

Post by catstevens »

one other question - say the input is seperated by line breaks. would i need to convert the line breaks to commas first?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
Post Reply