3

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.

Pradeep
  • 131
  • 1
  • 8

2 Answers2

2

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();
Jack
  • 21
  • 2
-1

Just set it to null before every prepare

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

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

$statement1=null;
$statement1 = $conn->prepare("SELECT * FROM test");
$exec_result1 = $statement1->execute();
while ($row1 = $statement1->fetchObject()) {   
   print_r($row1);
}