如何将 php 数组与 sql IN 运算符一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9618277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use php array with sql IN operator?
提问by Ahmad
I have and array with two values and I want to use it with sql IN operator in select query.
我有两个值的数组,我想在选择查询中将它与 sql IN 运算符一起使用。
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
有两个值的数组Array ( [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.
但它不会返回结果。
回答by barryhunter
As you have plain integerscan just do...
因为你有普通的整数可以做......
$sql = "SELECT * FROM table WHERE comp_id IN (".implode(',',$arr).")";
(as it keeps coming up, some additional information... )
(随着它不断出现,一些额外的信息......)
If working with with strings(particularly untrusted) input, can do
如果使用字符串(特别是不受信任的)输入,可以这样做
$sql = "SELECT * FROM table WHERE comp_id IN
('".implode("','",array_map('mysql_real_escape_string', $arr))."')";
but does not cope values like NULL. And will add quotes blindlyaround numeric values, which does notwork if using strict mysql mode. https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#idm140082377917056... ie ONLY use this if really working with strings (like VARCHAR), NOT numeric columns.
但不处理像 NULL 这样的值。并会添加引号盲目周围的数值,这并不会如使用MySQL的严格模式下工作。https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#idm140082377917056... 即只有在真正使用字符串(如 VARCHAR)时才使用它,而不是数字列。
The Needto call something like mysql_real_escape_string
is so that any quotes in the stringsis properly dealt with! (as well as preventing SQL Injections!)
在需要调用类似的东西mysql_real_escape_string
是这样,任何引号中字符串被妥善处理!(以及防止 SQL 注入!)
... if DO want to work with 'untrusted' numbers, can use intval
or floatval
...如果要使用“不受信任的”号码,可以使用intval
或floatval
$sql = "SELECT * FROM table WHERE comp_id IN (".implode(",",array_map('intval', $arr)).")";
to sanitise the input. (no quotes around the input.
对输入进行消毒。(输入周围没有引号。
回答by Aurimas
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));
回答by saba
You need to implode your array with ',' comma
你需要用','逗号来内爆你的数组
$imploded_arr = implode(',', $arr);
SELECT * from table Where comp_id IN ($imploded_arr)
回答by mishu
$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/implode
so before running the query try
所以在运行查询之前尝试
$arr = implode ( ', ', $arr);
回答by Chakradar Raju
you can only pass string to mysql as query, so try this
你只能将字符串作为查询传递给 mysql,所以试试这个
mysql_query("SELECT * FROM table WHERE comp_id IN (".implode(',',$arr).")");
回答by shazyriver
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. :)
希望它可以帮助像我这样的人。:)
回答by Jon Cairns
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.
请记住,这仅在数组包含整数时才有效。如果它们是字符串,则必须对每个元素进行转义。
回答by enygma
You need something like:
你需要这样的东西:
$sql = "SELECT * from table where comp_id in (".implode(',',$arr.")";
回答by jprofitt
You need to actually convert your $arr
to a string. The simplest way with what you're doing would be to use implode()
您需要实际将您的转换$arr
为字符串。你正在做的最简单的方法是使用implode()
$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"
现在,如果echo
您进行查询,您会看到IN
语句中不是数组,而是“数组”这个词
回答by MrCode
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)