MySQL的ID在数组中 [英] Mysql where id is in array
本文介绍了MySQL的ID在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ID字符串,例如1,2,3,4,5,我希望能够列出该ID中包含该列表的mysql中的所有行.
I have a string of ids like 1,2,3,4,5 and I want to be able to list all rows in mysql where the ID is contained in that list.
我认为最简单的方法是将字符串转换成数组,然后在($ array)中进行匹配,但这对我不起作用-没有错误等但它不返回行:
I assumed the easiest way would be to turn the string into an array and then match in ($array) but it doesn't work for me - no errors etc but it returns no rows:
$string="1,2,3,4,5";
$array=array_map('intval', explode(',', $string));
$query=mysqli_query($conn, "SELECT name FROM users WHERE id IN ('".$array."')");
如果我执行$ array的var_dump,则会得到:
If I do a var_dump of $array I get:
array(5) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
[3]=> int(4)
[4]=> int(5)
}
你知道我在哪里搞砸吗?
Any idea where I am screwing up?
推荐答案
$string="1,2,3,4,5";
$array=array_map('intval', explode(',', $string));
$array = implode("','",$array);
$query=mysqli_query($conn, "SELECT name FROM users WHERE id IN ('".$array."')");
NB:语法为:
SELECT * FROM table WHERE column IN('value1','value2','value3')
这篇关于MySQL的ID在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文