Mysql – how to resolve General error: 2014 Cannot execute queries while other unbuffered queries are active. using PDO connection

MySQLpdo

While i am executing second Stored procedure with same connection statement(Using PDO), getting the below error.

=================================================

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.

=======================================================

This is my code in drupal

$conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

$statement = $conn->prepare("CALL Odd_Get_Sport()");
$exec_result = $statement->execute();
while ($row = $statement->fetchObject()) {   
  print_r($row);
}


$statement ->closeCursor();

$statement1 = $conn->prepare("CALL Odd_Get_Sport()");
$exec_result1 = $statement1->execute();
while ($row1 = $statement1->fetchObject()) {   
   print_r($row1);
}

Help me on this.

Best Answer

This is a bit of a poor feature of PDO that's not well documented. The closeCursor method doesn't work when the statement has executed a stored procedure. You need to use the nextRowSet method. Here's what I use

            while($sth->nextRowSet())
        {
            $sth->fetchAll();
        }
        $sth->closeCursor();
Related Topic