1

I have table like this:

ID | title       | category
-----------------------------
1  | product1    | 0
2  | product2    | 0
3  | product3    | 1
4  | product4    | 1
5  | product5    | 3

I have array with ID's, how do I select products with those ID?

I mean something like this but category can be any value from array not only one value like in example.

$result = mysql_query("SELECT * FROM product where category = '$var'");
2

3 Answers 3

3

Try this way

$arr = array(1,2);
$str = implode(',',$arr);

$result = mysql_query("SELECT * FROM product where category IN (".$str.") ");

Note that: Try to avoid the MySQL extenstions, try to use PDO or prepared statements instead.

Sign up to request clarification or add additional context in comments.

2 Comments

mysql_query is deprecated.
@Petah thanks for reminding about deprecation
1
$ids = implode(',', $ids); // array(1,2,3,4) =>  '1,2,3,4'
$result = mysql_query("SELECT * FROM product WHERE category IN ('$ids')");

3 Comments

mysql_query is deprecated.
he asked how to do it in his case I gave him the answer what the problem?
Your answer will only lead to more problems
1

aside from out of date mysql_* functions and sql injection...

$result = mysql_query("SELECT * FROM product where category in ('$list_of_ids')");

2 Comments

mysql_query is deprecated.
i'm well aware of that and did link the op some other questions to make them aware too. the sql itself is the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.