Query Issues

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
User avatar
Weasel5-12
Forum Commoner
Posts: 37
Joined: Tue Sep 16, 2008 6:58 am

Query Issues

Post by Weasel5-12 »

I've got a question about a selection query, where i need to select a set of values based on parameters stored in an array.

Code: Select all

$query = "SELECT * FROM ingredientlist WHERE ingredientID='$ingredientID[]'";

i'd like the output query to resemble something similar to.. SELECT * FROM ingredientlist WHERE ingredientID='$ingredientID[1]' && ingredientID='$ingredientID[2]' && ingredientID='$ingredientID[3]' and so on...

how do i loop through this? i can't figure it out for the life of me..

thankyou very much for any replies
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Query Issues

Post by califdon »

Sounds like you want to use the OR operator:
...WHERE ingredientID='$ingredientID[1]' OR ingredientID='$ingredientID[2]' OR ingredientID='$ingredientID[3]'
or perhaps the IN operator:
...WHERE ingredientID IN('$ingredientID[1]', '$ingredientID[2]', '$ingredientID[3]')
User avatar
Weasel5-12
Forum Commoner
Posts: 37
Joined: Tue Sep 16, 2008 6:58 am

Re: Query Issues

Post by Weasel5-12 »

something like that, but i dont know how long the array will be though..

so how can i loop through it...?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Query Issues

Post by califdon »

Weasel5-12 wrote:something like that, but i dont know how long the array will be though..

so how can i loop through it...?

Code: Select all

foreach($ingredientID as $ing) {
    ....
}
Post Reply