Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP5 MySql and PDO Exception

Tags:

php

mysql

pdo

I have run into an Exception while using PDO with PHP5.4 and MySql.

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.

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.

Here is the 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.

Here are the parts of code that lead to this error:

First Part:

        $stmt = $this->dbConn->prepare("CALL getPopularProducts()");
        $stmt->execute();
        $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);

        //$stmt->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->applyProductCategory($p);

Second part:

        public function applyProductCategory(entity\Product $product) {
            $productId = $product->getProductId();
            try {
               /** exception thrown on this line **/
               $stmt = $this->dbConn->prepare("CALL getCategoryByProductId(?)"); 

               $stmt->bindParam(1, $productId, \PDO::PARAM_INT);
               $stmt->execute();

               while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
                   $category = new entity\ProductCategory();

                   $product->setCategory($category);
               }  
             }
             catch (\PDOException $e) {
                 echo $e->getMessage();
                 print($e->getTraceAsString());
             }
         }

Here is my database class:

namespace lib\database;

class Database
{
    protected static $instance;

    public static function getInstance()
    {
        if(self::$instance == null)
        {
            $dsn = "mydsn";
            $user = "myuser";
            $password = "mypassword";
            self::$instance->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
            self::$instance->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
            self::$instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        }
        return self::$instance;
    }

    private function __construct() {}

    private function __clone() {}
}

You may notice I have a call to $stmt->closeCursor() which I have commented out. When I include this line of code I get the following error:

SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

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...

So this error, happens for the first product in the loop only, i.e. the second time I call applyProductCategory() it works fine, no problem. The other thing is that below applyProductCategory are about 4 more calls to retrieve other types of associated data and there are never any errors thrown while doing that either.

So in the end, all my popular products are fine, except the very first product never has a ProductCategory associated with it.

Looking forward to finding out what I have missed along the way to cause this. Hopefully you can point me in the right directions.

like image 978
samazi Avatar asked Sep 25 '12 00:09

samazi


1 Answers

Trying using unset($stmt) right before where it's throwing the error

    unset($stmt);
    $stmt = $this->dbConn->prepare("CALL getCategoryByProductId(?)");  

I ran into the same error before and that fixed it for some reason. I found the answer here previously PDO Unbuffered queries

like image 148
FajitaNachos Avatar answered Oct 14 '22 03:10

FajitaNachos