Posted on 2009-02-18
Last Modified: 2013-12-12

I have this code. I successfully retrieve the data from the select statements. There are basically two (line 43 and 71) calls to stored procedure which are simply select statements with joins. However, this is the PDO Exception:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 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.' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs2\test_pdo3.php:73 Stack trace: #0 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs2\test_pdo3.php(73): PDOStatement->execute() #1 C:\Program Files\Zend\ZendStudio-5.5.0\bin\php5\dummy.php(1): include('C:\Program File...') #2 {main} thrown in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs2\test_pdo3.php on line 73

I know that if I set the the error mode to SILENT then I get a zero (0).

The solutions I've tried thus far:

Tried $db->closeCursor();
after the execute() as suggested in the link.

2) Am already using fetchAll().

3) Tried the buffered query statement as seen in the code.

This is what the respondent stated:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Selects in pdo_mysql are normally buffered which means that
before running a 2nd query you must either close the cursor or
fetch all records. Alternatively you can use buffered queries,
this is all expected behavior.

Any help is appreciated. Thank you,


class DBWriter extends PDO


	private $username;

	private $hostname;

	private $password;

	private $db;

	public function __construct()


		$username = 'root';

		$hostname = 'localhost';

		$password = 'htmys2007';

		$db = 'howtopr1_test';




$password, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));

	    	$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


	    catch (PDOException $e) {

  			throw new Exception("Failed to connect: " . $e->getMessage());

			/*** show the error info ***/

			foreach($dbh->errorInfo() as $error) {

		    	echo $error.'<br />';





$db = new DBWriter();

//$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);

$in_timestamp = '1233244800';

$in_classRoom_id = '1';


 *  first query


$sql = "CALL selectHour2($in_timestamp, $in_classRoom_id)";

$stmt = $db->prepare($sql);



/*$rows = $stmt>fetchAll(PDO::FETCH_NUM);

	if ($rows) {



/*** fetch the results ***/

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

$rowCount = $stmt->rowCount();

/*** loop of the results ***/

foreach($result as $row) {

	echo $row['splitYes'].'<br />';

	echo $row['FIRSTNAME'].'<br />';

	echo $row['LASTNAME'].'<br />';

	echo $row['firstName'].'<br />';

	echo $row['timeStamp'].'<br />';

	echo $row['classSize'].'<br />';

	echo $row['classType'].'<br />';


echo $rowCount.'<br />';


 * second query


$sql2 = "CALL selectCustDbFirstNames($in_timestamp, $in_classRoom_id)";

$stmt2 = $db->prepare($sql2);


/*** fetch the results ***/

$result2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);

$rowCount2 = $stmt2->rowCount();

/*** loop of the results ***/

foreach($result2 as $row2) {

	echo $row2['first_name'].'<br />';

	echo $row2['last_name'].'<br />';


echo $rowCount2;

/*** close the database connection ***/

//$db = null;


 * Optional feature of nextRowset() not functioning


/*$stmt2 = $db->query($sql);

do {

   $rows2 = $stmt2->fetchAll(PDO::FETCH_NUM);

   if ($rows2) {



} while ($stmt2->nextRowset());*/


Open in new window

Question by:Victor Kimura
    LVL 14

    Expert Comment

    the method closeCursor() should be applied to the statement (not the database)
    At line 55 add :

    Open in new window


    Accepted Solution

    Hello psadac,

    Doesn't work still. Any suggestions?
    LVL 14

    Expert Comment

    no idea. could you give your versions of php and mysql ? if you don't extend PDO class but use it directly does it work better ?

    additionally, but i don't think your problem is related to that , you should replace :

                    $username = 'root';

    by :

                    $this->username = 'root';


    Author Comment

    by:Victor Kimura
    Hi psadec,

    Apparently, this is a php/mysql bug. There is an update from mysql but only currently for php 5.3 or php 6.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now