0

I have an array of six elements ($categories = array('dinner','casual','wedding')) and I would like to make a SQL query that would look like this:

SELECT * FROM produts WHERE id = /* values of array $categories... eg. (dinner || casual || wedding) */

2
  • Are you trying to get the results for all items in the array in the same query or were you looking for separate queries for each item? Commented May 9, 2016 at 17:01
  • I want the results in the same query Commented May 9, 2016 at 17:08

2 Answers 2

1

Try this:

$conditions = '';

foreach($categories as $cat) {
    $conditions[] = " id = '".$cat."'";
}

$sql = 'SELECT * FROM produts WHERE '.implode(" OR ", $conditions);
Sign up to request clarification or add additional context in comments.

Comments

-1

You must to use IN instead WHERE.

2 Comments

"You must to use IN instead WHERE." - You still need to use WHERE with IN() dev.mysql.com/doc/refman/5.7/en/… as to what the OP wants to do here.
How would you apply your idea to the question example?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.