|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-01-07 13:35 UTC] bugs dot php dot net at andrewprendergast dot com
Description:
------------
PDO doesn't allow one to SELECT or DELETE groups of records
based on their primary key.
Specifically, the SQL clauses WHERE ... IN (...) and WHERE ... = ANY (...) does not work for more than one record.
As a workaround I can construct an SQL query and execute it myself but that defeats the niceness of PDO.
Reproduce code:
---------------
the following should return the records with ID 1 & 2:
$dbh = new PDO('mysql:host=localhost;dbname=mobop', "root", "");
$stmt = $dbh->prepare("SELECT * FROM news_item WHERE news_item_id IN(?)");
if ( $stmt->execute(array("1, 2")) )
while ( ($row = $stmt->fetch()) )
print_r($row);
But it doesn't.
The following execute statement fails as well:
$stmt->execute(array(array(2,1)))
NB: The intention of the 2nd example is that when binding an array, it seems natural that PDO would assume its part of an IN or =ANY clause and convert it into a bunch of comma separated keys automagically. Some of the higher level PDO based O/R abstractions currently bouncing around like Doctrine would then be able to support (without any modificaiton) queries that affect multiple records.
Expected result:
----------------
Two records should be loaded.
Actual result:
--------------
One record is loaded.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 11:00:01 2025 UTC |
>PDO does not implement query parsing it is entirely up to the database. Query parsing not needed, it can be done this way $stmt = $dbh->prepare("SELECT * FROM table WHERE id IN(:ids)"); $sth->bindValue(':ids', array(1,2,3), PDO::PARAM_ARRAY); If specified PDO::PARAM_ARRAY in bindValue method query can be parsed as this SELECT * FROM table WHERE id IN(1,2,3) not as SELECT * FROM table WHERE id IN('1,2,3') >The solution is to re-write queries to use IN (?, ?, �) It will not work if parameters array has variable length