id在数组中的Mysql [英] Mysql where id is in array
本文介绍了id在数组中的Mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像 1、2、3、4、5 这样的 ID 字符串,我希望能够列出 mysql 中 ID 包含在该列表中的所有行.
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)
}
知道我哪里搞砸了吗?
推荐答案
$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."')");
注意:语法是:
SELECT * FROM table WHERE column IN('value1','value2','value3')
这篇关于id在数组中的Mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文