I have and array with two values and I want to use it with sql IN operator in select query.
我有一个带有两个值的数组,我想将它与select query中的sql一起使用。
Here is the structure of my table
这是我的表格的结构
id comp_id
1 2
2 3
3 1
I have an array $arr
which have two values Array ( [0] => 1 [1] => 2 )
数组$arr有两个值数组([0]=> 1 [1]=> 2)
I want to fetch the record of comp_id 1 and comp_id 2. So I wrote the following query.
我想获取comp_id 1和comp_id 2的记录。所以我写了下面的查询。
SELECT * from table Where comp_id IN ($arr)
But it does not return the results.
但它不会返回结果。
10 个解决方案
#1
51
$sql = "SELECT * from table Where comp_id IN (".implode(',',$arr).")";
#2
12
you need to convert the array into comma-separated string:
需要将数组转换为逗号分隔的字符串:
$condition = implode(', ', $arr);
And, additionally, you might want to escape the values first (if you are unsure about the input):
此外,您可能希望首先转义值(如果您对输入不确定):
$condition = implode(', ', array_map('mysql_real_escape_string', $arr));
#3
4
$arr is a php array, to the sql server you need to send a string that will be parsed you need to turn your array in a list like 1, 2, etc..
$arr是一个php数组,您需要向sql服务器发送一个将被解析的字符串,您需要将数组转换为1、2等列表。
to do this you can use the function http://php.net/implode
为此,可以使用函数http://php.net/内爆
so before running the query try
所以在运行查询之前,请先尝试一下
$arr = implode ( ', ', $arr);
#4
4
You need to implode your array with ',' comma
需要用','逗号内爆数组
$imploded_arr = implode(',', $arr);
SELECT * from table Where comp_id IN ($imploded_arr)
#5
3
You're mixing PHP and SQL - for the IN
SQL operator, you need a format like:
您正在混合PHP和SQL—对于IN SQL操作符,您需要如下格式:
SELECT * from table WHERE comp_id IN (1,2)
So to get that in PHP you need to do something like:
为了得到PHP,你需要做如下操作:
$sql = "SELECT * from table Where comp_id IN (".implode(',',$arr).")"
Bear in mind that this only works if the array comprises of integers. You have to escape each element if they are strings.
记住,这只在数组由整数组成时才有效。如果每个元素都是字符串,就必须转义它们。
#6
3
you can only pass string to mysql as query, so try this
只能将字符串作为查询传递给mysql,请尝试一下
mysql_query("SELECT * FROM table WHERE comp_id IN (".implode(',',$arr).")");
#7
2
You need something like:
你需要什么东西:
$sql = "SELECT * from table where comp_id in (".implode(',',$arr.")";
#8
2
You need to actually convert your $arr
to a string. The simplest way with what you're doing would be to use implode()
您需要将$arr转换为字符串。最简单的方法是使用内爆()
$query = 'SELECT * from table Where comp_id IN (' . implode(',', $arr) . ')';
Right now if you echo
your query you'll see that rather than the array being in the IN
statement, it will just be the word "Array"
现在如果你回传你的查询你会看到不是in语句中的数组,而是单词" array "
#9
2
You need to convert the array to a string for use in the query:
您需要将数组转换为字符串,以便在查询中使用:
$list = implode(',', $arr);
Then it can be used in the IN clause:
然后可以在in子句中使用:
SELECT * from table Where comp_id IN ($list)
#10
2
All the people here are proposing the same thing but i got a warning in WordPress because of a simple error. You need to add commas to your imploded string. To be precise something like this.
这里所有的人都在建议同样的事情,但是我在WordPress中得到了一个警告,因为一个简单的错误。您需要向内爆字符串添加逗号。准确地说,是这样的。
$query = "SELECT *FROM table Where comp_id IN ( '" . implode( "', '", $sanitized_brands ) . "' )";
Hoping it helps someone like me. :)
希望它能帮助像我这样的人。:)
#1
51
$sql = "SELECT * from table Where comp_id IN (".implode(',',$arr).")";
#2
12
you need to convert the array into comma-separated string:
需要将数组转换为逗号分隔的字符串:
$condition = implode(', ', $arr);
And, additionally, you might want to escape the values first (if you are unsure about the input):
此外,您可能希望首先转义值(如果您对输入不确定):
$condition = implode(', ', array_map('mysql_real_escape_string', $arr));
#3
4
$arr is a php array, to the sql server you need to send a string that will be parsed you need to turn your array in a list like 1, 2, etc..
$arr是一个php数组,您需要向sql服务器发送一个将被解析的字符串,您需要将数组转换为1、2等列表。
to do this you can use the function http://php.net/implode
为此,可以使用函数http://php.net/内爆
so before running the query try
所以在运行查询之前,请先尝试一下
$arr = implode ( ', ', $arr);
#4
4
You need to implode your array with ',' comma
需要用','逗号内爆数组
$imploded_arr = implode(',', $arr);
SELECT * from table Where comp_id IN ($imploded_arr)
#5
3
You're mixing PHP and SQL - for the IN
SQL operator, you need a format like:
您正在混合PHP和SQL—对于IN SQL操作符,您需要如下格式:
SELECT * from table WHERE comp_id IN (1,2)
So to get that in PHP you need to do something like:
为了得到PHP,你需要做如下操作:
$sql = "SELECT * from table Where comp_id IN (".implode(',',$arr).")"
Bear in mind that this only works if the array comprises of integers. You have to escape each element if they are strings.
记住,这只在数组由整数组成时才有效。如果每个元素都是字符串,就必须转义它们。
#6
3
you can only pass string to mysql as query, so try this
只能将字符串作为查询传递给mysql,请尝试一下
mysql_query("SELECT * FROM table WHERE comp_id IN (".implode(',',$arr).")");
#7
2
You need something like:
你需要什么东西:
$sql = "SELECT * from table where comp_id in (".implode(',',$arr.")";
#8
2
You need to actually convert your $arr
to a string. The simplest way with what you're doing would be to use implode()
您需要将$arr转换为字符串。最简单的方法是使用内爆()
$query = 'SELECT * from table Where comp_id IN (' . implode(',', $arr) . ')';
Right now if you echo
your query you'll see that rather than the array being in the IN
statement, it will just be the word "Array"
现在如果你回传你的查询你会看到不是in语句中的数组,而是单词" array "
#9
2
You need to convert the array to a string for use in the query:
您需要将数组转换为字符串,以便在查询中使用:
$list = implode(',', $arr);
Then it can be used in the IN clause:
然后可以在in子句中使用:
SELECT * from table Where comp_id IN ($list)
#10
2
All the people here are proposing the same thing but i got a warning in WordPress because of a simple error. You need to add commas to your imploded string. To be precise something like this.
这里所有的人都在建议同样的事情,但是我在WordPress中得到了一个警告,因为一个简单的错误。您需要向内爆字符串添加逗号。准确地说,是这样的。
$query = "SELECT *FROM table Where comp_id IN ( '" . implode( "', '", $sanitized_brands ) . "' )";
Hoping it helps someone like me. :)
希望它能帮助像我这样的人。:)