Jump to content

array in query


1internet
Go to solution Solved by 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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.