My code:
我的代码:
$myArray = implode($myArray, ',');
$sth = $dbh->prepare('SELECT foo FROM bar WHERE ids IN (:ids)');
$sth->bindParam(':ids', $myArray);
$sth->execute();
$result = $sth->fetch();
echo $sth->rowCount();
Always shows a count of 1, but when I skip the parametrization and just add the variable itself in it's place, I get an accurate count. What's going on here?
始终显示计数为1,但是当我跳过参数化并只是将变量本身添加到其中时,我得到一个准确的计数。这里发生了什么?
3 个解决方案
#1
12
You can't bind a parameter for the IN clause like that. The $myArray string will only count as one value, like if you did this:
您不能像这样绑定IN子句的参数。 $ myArray字符串只计为一个值,就像你这样做:
SELECT foo FROM bar WHERE ids IN ('1,2,3')
Even though there are three comma delimited values, the database reads them as only one string value.
即使有三个逗号分隔值,数据库也只读取一个字符串值。
You need to manually insert the IN list into the query, the old-school way.
您需要手动将IN列表插入到查询中,即旧式方式。
'SELECT foo FROM bar WHERE ids IN (' . $myArray .')'
There is unfortunately no other way. At least for now.
遗憾的是没有别的办法。最起码到现在。
#2
6
I know this question is old, but this works for me:
我知道这个问题很老,但这对我有用:
$arrayOfValues = array(1,2,3,4,5);
$questionMarks = join(",", array_pad(array(), count($arrayOfValues), "?"));
$stmt = $dbh->prepare("update some_table set end_date = today where value_no in ($questionMarks)");
$stmt->execute($arrayOfValues);
#3
0
Why dont you use this solution?
为什么不使用这个解决方案?
https://*.com/a/39353278/1834212
https://*.com/a/39353278/1834212
It parses an array of values which are the pairs of bind placeholders and its elements, in the case of a IN you just need to feed an Array , and then it will parse the array and modify the string for you using bind names generated on the fly, and then feed those values of the array
它解析一组值,这些值是绑定占位符及其元素对,在IN的情况下,您只需要提供一个数组,然后它将解析数组并使用生成的绑定名称为您修改字符串飞,然后提供数组的这些值
#1
12
You can't bind a parameter for the IN clause like that. The $myArray string will only count as one value, like if you did this:
您不能像这样绑定IN子句的参数。 $ myArray字符串只计为一个值,就像你这样做:
SELECT foo FROM bar WHERE ids IN ('1,2,3')
Even though there are three comma delimited values, the database reads them as only one string value.
即使有三个逗号分隔值,数据库也只读取一个字符串值。
You need to manually insert the IN list into the query, the old-school way.
您需要手动将IN列表插入到查询中,即旧式方式。
'SELECT foo FROM bar WHERE ids IN (' . $myArray .')'
There is unfortunately no other way. At least for now.
遗憾的是没有别的办法。最起码到现在。
#2
6
I know this question is old, but this works for me:
我知道这个问题很老,但这对我有用:
$arrayOfValues = array(1,2,3,4,5);
$questionMarks = join(",", array_pad(array(), count($arrayOfValues), "?"));
$stmt = $dbh->prepare("update some_table set end_date = today where value_no in ($questionMarks)");
$stmt->execute($arrayOfValues);
#3
0
Why dont you use this solution?
为什么不使用这个解决方案?
https://*.com/a/39353278/1834212
https://*.com/a/39353278/1834212
It parses an array of values which are the pairs of bind placeholders and its elements, in the case of a IN you just need to feed an Array , and then it will parse the array and modify the string for you using bind names generated on the fly, and then feed those values of the array
它解析一组值,这些值是绑定占位符及其元素对,在IN的情况下,您只需要提供一个数组,然后它将解析数组并使用生成的绑定名称为您修改字符串飞,然后提供数组的这些值