mysql 删除id在数组中,MySQL的ID在数组中

博客讲述了在MySQL中如何从包含多个ID的字符串中正确地查询相关记录。作者遇到了问题,即使用IN操作符并直接传递数组时没有返回结果。解决方案是先将字符串转换为用逗号分隔的格式,然后在查询中使用该格式。修复后的查询能够成功匹配到数据库中的相应行。
摘要由CSDN通过智能技术生成

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.

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."')");

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: the syntax is:

SELECT * FROM table WHERE column IN('value1','value2','value3')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值