MySQL的PHP​​ - 选择ID =阵列()? [英] MySQL PHP - SELECT WHERE id = array()?

查看:86
本文介绍了MySQL的PHP​​ - 选择ID =阵列()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  使用数组结果 MySQL查询
  
传递一个数组到MySQL

我在PHP中的数组:

$array = array(1, 4, 5, 7);

正如你所看到的,我有不同的价值观的数组,但我想要写一个MYSQL语句,将检查 ID 等于以任何数组中的值。例如,如果一个行具有 ID 1,这将返回该行,同样对4,5和7中的阵列的长度可因变化该计划的性质,所以这就是问题的所在。我只是做:

As you can see, I have an array of different values, but I want to write a MYSQL statement that will check if the id is equal to any of the values in the array. For example, if a row has an id of 1, it will return that row, the same for 4, 5, and 7. The length of the array can vary due to the nature of the program, so that's where the problem is. Could I just do:

SELECT ...
  FROM ...
 WHERE id = '$array'

还是有更好的办法?结果
如果我是不清楚,请让我获得更多信息。

Or is there a better way?
If I was unclear, please ask me for more info.

推荐答案

使用

$sql = 'SELECT * 
          FROM `table` 
         WHERE `id` IN (' . implode(',', array_map('intval', $array)) . ')';

这篇关于MySQL的PHP​​ - 选择ID =阵列()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆