Fixing MySQL / PDO error 2014

The following error on my current project at work really gave me lots of headaches today:

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.

So, yes, I already have PDO::MYSQL_ATTR_USE_BUFFERED_QUERY set to TRUE, so why is PDO still complaining? And especially why it is complaining now, because the same code which triggered the error today ran without problems for the past 9 months?

After struggling a lot I found the cause: I missed to close a statement which was reused in a loop!

So take care that you always call PDOStatement->closeCursor() in use cases like this:

$stmt = $con->prepare(“SELECT * FROM doodle WHERE id = ?”);

foreach (range(1,10) as $id)
{
$stmt->execute(array($id));
$row = $stmt->fetch();
$stmt->closeCursor();
}

2 thoughts on “Fixing MySQL / PDO error 2014”

  1. What does this driver different from the original one? And other than that, it wouldn’t help me anyways because I’m not in a Windows environment…

Comments are closed.