IN() with string as parameter not working with PDO

谁说我不能喝 提交于 2019-12-25 03:26:34

问题


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) ...");

This works fine with the mysql_query statement, but it will not work with PDO prepare and execute (no results are returned).

What am I missing here?


回答1:


If the query sql works fine with mysql_query, then it will work fine with pdo.

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);



回答2:


Please refer to either PDO binding values for MySQL IN statement or Can I bind an array to an IN() condition?.

I think that is a solution you are looking for since you cannot bind multiple values to a single named parameter in.




回答3:


"You cannot bind multiple values to a single named parameter in, for example, the IN() clause of an SQL statement."

http://php.net/manual/en/pdo.prepare.php

That goes against the whole purpose of prepared statements.



来源:https://stackoverflow.com/questions/11405602/in-with-string-as-parameter-not-working-with-pdo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!