|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38001 In PDO mysql running multiple queries with one exec, subsequent query fails
Submitted: 2006-07-04 09:12 UTC Modified: 2009-09-29 21:54 UTC
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:0 (0.0%)
From: mark-phpbugs at vectrex dot org dot uk Assigned:
Status: Closed Package: PDO related
PHP Version: 5.1.4 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: mark-phpbugs at vectrex dot org dot uk
New email:
PHP Version: OS:


 [2006-07-04 09:12 UTC] mark-phpbugs at vectrex dot org dot uk
When running multiple queries with one exec, the queries
succeed (or appear to succeed, with no exception), but a
subsequent query fails with 2013 Lost connection to MySQL server during query

I'm using MySQL 5.0.20a-Debian_2-log on Debian Linux. Client library version is 

"PDO Driver for MySQL, client library version => 5.0.22"

This is similar to "bogus" bug #37732 which affects mysqli - But in PDO there is no way to manually "consume" the empty result sets from an exec() - PDO should really do it automatically.

Reproduce code:

function DbInit()
	global $db;
	$options = array();
	$connstr = "mysql:host=localhost;dbname=test";
	$db = new PDO($connstr, "root", "", $options);
	// Make sure our connections use utf8.
	$db->exec("SET NAMES utf8");

function TestBatch()
	global $db;
	$sql = "CREATE TEMPORARY table b (t varchar(20))";
	$sql2 = " INSERT INTO b VALUES ('hello')";
	/* This works
	/* This fails because the subsequent query gives 2013 Lost connection to MySQL server during query */
	$db->exec($sql . ";" . $sql2);	
	$sth = $db->query("SELECT * FROM b");



Expected result:
array(1) {
  array(1) {
    string(5) "hello"

Actual result:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2013 Lost connection to MySQL server during query' in /home/mark/progs/phptest/pdobatch.php:30
Stack trace:
#0 /home/mark/progs/phptest/pdobatch.php(30): PDO->query('SELECT * FROM b')
#1 /home/mark/progs/phptest/pdobatch.php(35): TestBatch()
#2 {main}
  thrown in /home/mark/progs/phptest/pdobatch.php on line 30


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-04 19:40 UTC]
Please try using this CVS snapshot:
For Windows:

Works for me using MySQ 5.0.22
 [2006-07-04 22:11 UTC] mark-phpbugs at vetrex dot org dot uk
I've tried this with the latest snapshot, php5.2-200607042030, and the problem remains.
 [2006-07-12 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2008-07-16 10:47 UTC] andrea at bhweb dot it
Although nasty, I found a workaround, like the one used in mysqli to 
consume the resultsets:

do { $stmt->fetch(); $stmt->closeCursor(); ++$line; } while($stmt-

I found this only works using prepare and execute this way, not if you 
directly execute the query with query().

 [2008-07-16 22:16 UTC]
Multi-query is a MySQL specific feature and the PDO API does not support it properly. Multi-query are a potential security risk as they make SQL injections easier. This is against the idea of PDO to be secure. Check this article for details . 

PDO_MYSQLND will fix the bug:

nixnutz@ulflinux:~/php53> sapi/cli/php -r '$db = new PDO("mysql:host=localhost;dbname=test", "root", "root");  $db->exec("DROP TABLE IF EXISTS test; CREATE TABLE test(id INT); INSERT INTO test(id) VALUES(1)"); $stmt = $db->query("SELECT * FROM test"); var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); var_dump($db->errorInfo());'
array(1) {
  array(1) {
    string(1) "1"
array(1) {
  string(5) "00000"

 [2009-09-29 21:54 UTC]
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at

Fixed in PHP 5.3+
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Mar 31 20:03:38 2023 UTC