通过php在多个id中选择数据库中的表记录

i have a value that name is brands and like this :

$brands = "1,2,3,4,5,";

i can convert this value to this :

$brands = "1,2,3,4,5";

i want write source that select * from table where id = each of brands

for example :

select * from brands where (id='1' or id='2' or id='3' or id='4' or id='5');

1,2,3,4,5 is Variable and can changed for example can change to 1,2,9,4,5,8

how i can write this ? please help thanks

If you already have the ids comma delimited...

$query = '
   SELECT *
     FROM `brands`
    WHERE `id` IN (' . $brands . ')';

If not, use implode(',', $ids).

If your ids are from user input and not already made safe, use...

$ids = implode(',', array_map('intval', explode(',', $ids)));

"select * from brands where id in (".$brands.");"