{"id":162,"date":"2016-12-03T12:44:22","date_gmt":"2016-12-03T12:44:22","guid":{"rendered":"http:\/\/iain.rauch.co.uk\/blog\/?p=162"},"modified":"2016-12-03T12:44:22","modified_gmt":"2016-12-03T12:44:22","slug":"php-pdo-how-to-solve-cannot-execute-queries-while-other-unbuffered-queries-are-active","status":"publish","type":"post","link":"https:\/\/iain.rauch.co.uk\/blog\/2016-12\/php-pdo-how-to-solve-cannot-execute-queries-while-other-unbuffered-queries-are-active\/","title":{"rendered":"PHP PDO: How to solve &#8220;Cannot execute queries while other unbuffered queries are active&#8221;"},"content":{"rendered":"<p>While migrating one of my hobby projects from the PHP mysql extension to PDO, I came across this error:<\/p>\n<blockquote><p><tt>PHP Fatal error: \u00a0Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. \u00a0Consider using PDOStatement::fetchAll(). \u00a0Alternatively, 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.'<\/tt><\/p><\/blockquote>\n<p>A quick search on the web suggested this happens when you don&#8217;t fetch all rows from a query. I knew this wasn&#8217;t the case and didn&#8217;t want to just enable the buffered query attribute as I felt something else was wrong.<\/p>\n<p>Turns out this problem came about as I was trying to migrate my MySQL connection properties, previously defined with:<\/p>\n<p>[code lang=&#8221;php&#8221;]define(&#8216;UTC_OFFSET&#8217;, date(&#8216;P&#8217;));<br \/>\nmysql_query(&#8220;SET time_zone='&#8221; . UTC_OFFSET . &#8220;&#8216;;&#8221;);<br \/>\nmysql_query(&#8220;SET NAMES &#8216;utf8&#8217; COLLATE &#8216;utf8_general_ci;'&#8221;);[\/code]<\/p>\n<p>The natural change was to add these two statements to <tt>PDO::MYSQL_ATTR_INIT_COMMAND<\/tt> (separated by a semicolon). However, that&#8217;s where the problem is. The <tt><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/set-statement.html\" target=\"_blank\">SET<\/a><\/tt> command allows\u00a0both to be specified at once, hence\u00a0the right way of doing it is:<\/p>\n<p>[code lang=&#8221;php&#8221;]PDO::MYSQL_ATTR_INIT_COMMAND => &#8220;SET NAMES &#8216;utf8&#8217; COLLATE &#8216;utf8_general_ci&#8217;, time_zone = &#8216;&#8221; . UTC_OFFSET . &#8220;&#8216;&#8221;[\/code]<\/p>\n<p>Credit: <a href=\"http:\/\/stackoverflow.com\/questions\/17434102\/causes-of-mysql-error-2014-cannot-execute-queries-while-other-unbuffered-queries#comment59663977_35141881\" target=\"_blank\">Stack Overflow<\/a><\/p>\n<p>Bonus:\u00a0Setting the timezone with a UTC offset allows you to use a zone that PHP knows about, but the MySQL server doesn&#8217;t. That way it can be set with the ini setting <tt>date.timezone<\/tt> or <tt><a href=\"http:\/\/php.net\/manual\/en\/function.date-default-timezone-set.php\" target=\"_blank\">date_default_timezone_set<\/a><\/tt> and doesn&#8217;t need to be modified in two places if it needs to be changed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While migrating one of my hobby projects from the PHP mysql extension to PDO, I came across this error: PHP Fatal error: \u00a0Uncaught exception &#8216;PDOException&#8217; with message &#8216;SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. &hellip; <a href=\"https:\/\/iain.rauch.co.uk\/blog\/2016-12\/php-pdo-how-to-solve-cannot-execute-queries-while-other-unbuffered-queries-are-active\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-162","post","type-post","status-publish","format-standard","hentry","category-development"],"_links":{"self":[{"href":"https:\/\/iain.rauch.co.uk\/blog\/wp-json\/wp\/v2\/posts\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iain.rauch.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iain.rauch.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iain.rauch.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iain.rauch.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":6,"href":"https:\/\/iain.rauch.co.uk\/blog\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":168,"href":"https:\/\/iain.rauch.co.uk\/blog\/wp-json\/wp\/v2\/posts\/162\/revisions\/168"}],"wp:attachment":[{"href":"https:\/\/iain.rauch.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iain.rauch.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iain.rauch.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}