title: PHP5 MySql and PDO Exception
body: <p>I have run into an Exception while using PDO with PHP5.4 and MySql.</p>
  
  <p>I have read almost every other post, blog, manual, snippet, everything I can related to this error and have tried everything I have seen with no luck.</p>
  
  <p>So I am hoping it is specific to something I have done wrong in my particular code and you can point it out to me.</p>
  
  <p>Here is the error:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>Here are the parts of code that lead to this error:</p>
  
  <p><strong>First Part:</strong></p>
  
  <pre><code>        $stmt = $this-&gt;dbConn-&gt;prepare("CALL getPopularProducts()");
          $stmt-&gt;execute();
          $rows = $stmt-&gt;fetchAll(\PDO::FETCH_ASSOC);
  
          //$stmt-&gt;closeCursor();
  
          foreach ($rows as $row) {
              $p = new entity\LightingPole();
               /* doing basic set stuff here so code omitted */
  
              // apply category to product
              $categroyTranslator = new ProductCategoryTranslator();
              $categroyTranslator-&gt;applyProductCategory($p);
  </code></pre>
  
  <p><strong>Second part:</strong></p>
  
  <pre><code>        public function applyProductCategory(entity\Product $product) {
              $productId = $product-&gt;getProductId();
              try {
                 /** exception thrown on this line **/
                 $stmt = $this-&gt;dbConn-&gt;prepare("CALL getCategoryByProductId(?)"); 
  
                 $stmt-&gt;bindParam(1, $productId, \PDO::PARAM_INT);
                 $stmt-&gt;execute();
  
                 while ($row = $stmt-&gt;fetch(\PDO::FETCH_ASSOC)){
                     $category = new entity\ProductCategory();
  
                     $product-&gt;setCategory($category);
                 }  
               }
               catch (\PDOException $e) {
                   echo $e-&gt;getMessage();
                   print($e-&gt;getTraceAsString());
               }
           }
  </code></pre>
  
  <p><strong>Here is my database class:</strong></p>
  
  <pre><code>namespace lib\database;
  
  class Database
  {
      protected static $instance;
  
      public static function getInstance()
      {
          if(self::$instance == null)
          {
              $dsn = "mydsn";
              $user = "myuser";
              $password = "mypassword";
              self::$instance-&gt;setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
              self::$instance-&gt;setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
              self::$instance-&gt;setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
          }
          return self::$instance;
      }
  
      private function __construct() {}
  
      private function __clone() {}
  }
  </code></pre>
  
  <p>You may notice I have a call to <code>$stmt-&gt;closeCursor()</code> which I have commented out. When I include this line of code I get the following error:</p>
  
  <blockquote>
    <p>SQLSTATE[HY000]: General error: 2006 MySQL server has gone away</p>
  </blockquote>
  
  <p>Now to explain it in plain functionality terms, what I am basically doing here is getting a list of popular products, looping through them and getting all the associated product data such as ProductCategory etc...</p>
  
  <p>So this error, happens for the first product in the loop only, i.e. the second time I call <code>applyProductCategory()</code> it works fine, no problem. The other thing is that below <code>applyProductCategory</code> are about 4 more calls to retrieve other types of associated data and there are never any errors thrown while doing that either.</p>
  
  <p>So in the end, all my popular products are fine, except the very first product never has a ProductCategory associated with it.</p>
  
  <p>Looking forward to finding out what I have missed along the way to cause this. Hopefully you can point me in the right directions.</p>