MySQL PHP -选择id = array()?(复制)

时间:2022-09-18 14:23:22

Possible Duplicate:
MySQL query using an array
Passing an array to mysql

可能的重复:使用数组向MySQL传递数组的MySQL查询

I have an array in PHP:

我有一个PHP数组:

$array = array(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:

如您所见,我有一个不同值的数组,但是我想编写一个MYSQL语句来检查id是否等于数组中的任何值。例如,如果一行的id为1,它将返回这一行,4、5和7也是相同的。由于程序的性质,数组的长度可能会有所不同,所以这就是问题所在。我只是能做:

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

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

或者有更好的方法吗?如果我不清楚,请问我更多的信息。

6 个解决方案

#1


92  

Use IN.

在使用。

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

#2


21  

What you are looking for is the IN() statement. This will test if a given field contains any of 1 or more values.

您需要的是IN()语句。这将测试给定字段是否包含一个或多个值。

SELECT * FROM `Table` WHERE `id` IN (1, 2, 3)

You can use a simple loop to convert your $array variable into the proper format. Be sure to be mindful of SQL injection if your array values are coming from the front end.

您可以使用一个简单的循环将$array变量转换为正确的格式。如果数组值来自前端,请务必注意SQL注入。

#3


10  

Simply use the ID IN () syntax:

只需在()语法中使用ID:

$sql = "SELECT FROM `table` WHERE `ID` IN (".implode(',',$array).")";

#4


0  

You can write your query like this:

您可以这样编写查询:

select * from table where id in (1, 4, 6, 7)

You can add as many values as you need.

您可以添加任意数量的值。

#5


0  

You can SELECT WHERE id IN (item1, item2...). Just loop through your PHP array to build your array for the query.

您可以选择id在哪里(item1, item2…)。只需循环PHP数组,为查询构建数组。

#6


0  

You'll want to use the IN operator:

你需要使用IN操作符:

$sql = 'SELECT FROM WHERE id IN (' . implode(",", $array) . ')';

$sql = 'SELECT FROM WHERE id IN (')内爆(”、“美元数组)。“)”;

There may be a limit in MySQL on how many values you can use in a list with the IN operator.

MySQL中可能会有一个限制,在一个列表中,有多少个值可以用于操作符。

#1


92  

Use IN.

在使用。

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

#2


21  

What you are looking for is the IN() statement. This will test if a given field contains any of 1 or more values.

您需要的是IN()语句。这将测试给定字段是否包含一个或多个值。

SELECT * FROM `Table` WHERE `id` IN (1, 2, 3)

You can use a simple loop to convert your $array variable into the proper format. Be sure to be mindful of SQL injection if your array values are coming from the front end.

您可以使用一个简单的循环将$array变量转换为正确的格式。如果数组值来自前端,请务必注意SQL注入。

#3


10  

Simply use the ID IN () syntax:

只需在()语法中使用ID:

$sql = "SELECT FROM `table` WHERE `ID` IN (".implode(',',$array).")";

#4


0  

You can write your query like this:

您可以这样编写查询:

select * from table where id in (1, 4, 6, 7)

You can add as many values as you need.

您可以添加任意数量的值。

#5


0  

You can SELECT WHERE id IN (item1, item2...). Just loop through your PHP array to build your array for the query.

您可以选择id在哪里(item1, item2…)。只需循环PHP数组,为查询构建数组。

#6


0  

You'll want to use the IN operator:

你需要使用IN操作符:

$sql = 'SELECT FROM WHERE id IN (' . implode(",", $array) . ')';

$sql = 'SELECT FROM WHERE id IN (')内爆(”、“美元数组)。“)”;

There may be a limit in MySQL on how many values you can use in a list with the IN operator.

MySQL中可能会有一个限制,在一个列表中,有多少个值可以用于操作符。