php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36347 PDO::exec() fails if the query returns results
Submitted: 2006-02-10 01:24 UTC Modified: 2006-02-16 16:23 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: david at acz dot org Assigned: george (profile)
Status: Not a bug Package: PDO related
PHP Version: 5.1.2 OS: SuSE Linux 9.3
Private report: No CVE-ID: None
 [2006-02-10 01:24 UTC] david at acz dot org
Description:
------------
[Note: I am actually testing this on PHP 5.1.1.  If this bug was fixed in PHP 5.1.2, please add a note to the manual page for PDO::exec()].

The manual says:

"PDO::exec() does not return results from a SELECT statement. For a SELECT statement that you only need to issue once during your program, consider issuing PDO::query()."

Either the manual needs to be changed, or, ideally, PDO::exec() needs to be fixed to discard results.

This issue has bit me multiple times.  It's easy to forget that a certain query (such as MySQL's OPTIMIZE TABLE) will return a result.  Using PDO::exec() in such cases causes an error later that can be difficult to track down.

Reproduce code:
---------------
<?
     $db = new PDO("mysql:host=localhost;dbname=test");

     $db->exec("SELECT 1");

     $st = $db->prepare("SELECT NOW()");
     if ($st === false)
     {
         $e = $db->errorInfo();
         echo "$e[0]:$e[1]: $e[2]\n";
     }
?>


Expected result:
----------------
[nothing]

Actual result:
--------------
HY000:2014: Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-02-10 02:34 UTC] david at acz dot org
This bug is similar to #34499.  I can't comment on that, so I'm commenting here:

"OPTIMIZE TABLE is a query that returns rows.
You should use PDO::query() instead.
I'll see about handling this user error more gracefully."

You actually can't use PDO::query() with OPTIMIZE TABLE:

HY000:2030:This command is not supported in the prepared statement protocol yet

What is the solution?
 [2006-02-10 15:18 UTC] tony2001@php.net
Assigned to the maintainer.
 [2006-02-14 15:13 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

exec() method is intended only for one time execution of queries that do not return any records. There is no bug here.
 [2006-02-14 16:05 UTC] david at acz dot org
See my previous comment.  There is no way to execute "OPTIMIZE TABLE" with MySQL using PDO.
 [2006-02-16 16:23 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

In new version of PDO mysql you can safely use exec() method. It returns 0 because now rows were affected, but the operation is successful (on failure FALSE is returned).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 10:01:29 2024 UTC