Uncaught exception ‘PDOException’:General error: 2014

I received this error while I was testing out a prepared statement using PDO. This happened when criteria from a textbox added to the query’s WHERE clause within the prepared statement.

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.’ path-to-file/filename.php: +280 Stack trace: #0 path-to-file/filename.php (280):PDO->prepare(‘SELECT * FROM B…’) #1 {main} thrown in /path-to-file/filename.php on line {LINE#}

Turns out the connection string needed an attribute set so adding the line below solved the issue:
$conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);

Now the connection string looks as follows:

$dbhost = ‘ipaddress’
$dbname = ‘database_name’
$dbuser = ‘user_name’
$dbpass = ‘password’
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
//added this line to set the MYSQL_ATTR_USE_BUFFERED_QUERY attribute
 $conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);

and then all was good


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>