使用字符串作为参数的IN()无法与PDO一起使用 [英] IN() with string as parameter not working with PDO
问题描述
我正在尝试将其中包含以下内容的mysql_query语句转换为PDO:
I'm trying to convert a mysql_query statement that had the following in it to PDO:
$string = '2, 3, 4, 5, 7, 8';
mysql_query(" ... IN($string) ...");
mysql_query(" ... IN($string) ...");
这对于mysql_query
语句可以正常工作,但不适用于PDO准备和执行(不返回任何结果).
This works fine with the mysql_query
statement, but it will not work with PDO prepare and execute (no results are returned).
我在这里想念什么?
推荐答案
如果查询sql在mysql_query
上运行正常,那么它将在pdo上运行正常.
If the query sql works fine with mysql_query
, then it will work fine with pdo.
如果仅用一个占位符绑定整个字符串,那将是行不通的.
您需要为IN
子句的每个值绑定.
What will not work is if you bind the whole string with just one placeholder.
You need to bind for each values for the IN
clause.
示例:
Example:
$string = '2, 3, 4, 5, 7, 8';
$params = explode(', ', $string);
$sth = $pdo->prepare(' ... IN('.join(',', array_fill(0, count($params), '?')).') ...');
$ret = $sth->execute($params);
这篇关于使用字符串作为参数的IN()无法与PDO一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!