通过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
我有一个名称为品牌的值,如下所示: p>
$ brands =“1,2,3,4,5”;; code> pre>我可以将此值转换为: p> \ n
$ brands =“1,2,3,4,5”; code> pre>
我想要从表中选择*的写源 =每个品牌 p>
例如: p>
从品牌中选择*(id ='1'或id ='2'或 id ='3'或id ='4'或id ='5'); code> pre>
1,2,3,4,5是变量并且可以更改 例如可以改为1,2,9,4,5,8 p>
我怎么写这个? 请帮助谢谢 p> div>
答
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.");"