php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40048 SQL IN (...) and = ANY (...) not supported by PDO
Submitted: 2007-01-07 13:35 UTC Modified: 2007-01-08 00:23 UTC
From: bugs dot php dot net at andrewprendergast dot com Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.2.1RC2 OS: Linux Red Hat EL
Private report: No CVE-ID: None
 [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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-07 17:44 UTC] iliaa@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

PDO does not implement query parsing it is entirely up to the 
database. So the features you are talking about would only 
work if supported by the underlying database. 
 [2007-01-07 23:46 UTC] nlopess@php.net
I also came across this problem last week: it would be could if we could bind an array to IN(). Probably it would be too dificult, but we could support this syntax in our sql re2c lexer..
 [2007-01-08 00:23 UTC] bugs dot php dot net at andrewprendergast dot com
Just to re-iterate some comments made about this on IRC:

<ilia> again PDO is not design to "modify" or extend SQL query language

<Pierre> about definition, PDO is "only" a wrapper not an abstraction layer, it does not abstract anything (besides

Specifically PDO does not do any parsing of SQL queries, it just passes them directly to the database engine in question so PDO is unable to re-write the SQL to support this feature.

The solution is to re-write queries to use IN (?, ?, ?)
 [2011-05-30 16:52 UTC] ggrraay at gmail dot com
>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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 09:01:28 2024 UTC