Jump to content

array in query


1internet

Recommended Posts

I want to list results that belong to a certain category. However, I have multiple categories and some results can belong to more than one cateogry.

I want to a query that will show all the results from several categories, the number of categories will be aribitrary though.

So I will have the categories contained within an array e.g.

$cat_ids = array(12,26,32)

And I want the query to be

$sql = "SELECT * FROM `categories` WHERE `cat_id`=12 OR `cat_id`=26 OR `cat_id`=32";

Of course sometimes there may only be 1 category, and sometimes there could be 5. So how do you execute the array in the query?

Or do I have to run a foreach loop?

$sql = "SELECT * FROM `categories` WHERE"
$i = 0;
foreach ($cat_ids AS $cat_id){
 if($i!=0) $sql = "OR "
  $sql = "`cat_id`=$cat_id"
 $i++
}
Link to comment
https://forums.phpfreaks.com/topic/281653-array-in-query/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.