PHP5 MySql 和 PDO 异常

标签 php mysql pdo

我在将 PDO 与 PHP5.4 和 MySql 一起使用时遇到异常。

我已经阅读了几乎所有其他帖子、博客、手册、片段,以及与此错误相关的所有内容,并且尝试了我所看到的所有内容,但没有成功。

所以我希望它是特定于我在我的特定代码中做错的事情,你可以向我指出。

这里是错误:

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.

以下是导致此错误的代码部分:

第一部分:

        $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);

第二部分:

        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());
             }
         }

这是我的数据库类:

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() {}
}

您可能会注意到我调用了已注释掉的 $stmt->closeCursor()。当我包含这行代码时,出现以下错误:

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

现在用简单的功能术语来解释它,我在这里基本上做的是获取流行产品的列表,遍历它们并获取所有相关的产品数据,例如 ProductCategory 等...

所以这个错误只发生在循环中的第一个产品上,即我第二次调用 applyProductCategory() 它工作正常,没问题。另一件事是,在 applyProductCategory 下面还有大约 4 个调用来检索其他类型的关联数据,并且在执行这些操作时也不会抛出任何错误。

所以最后,我所有受欢迎的产品都很好,除了第一个产品从来没有与之关联的 ProductCategory。

期待找出我在造成这种情况的过程中遗漏了什么。希望您能为我指明正确的方向。

最佳答案

尝试在抛出错误之前使用 unset($stmt)

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

我之前遇到过同样的错误,但出于某种原因修复了它。我之前在这里找到了答案 PDO Unbuffered queries

关于PHP5 MySql 和 PDO 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12574676/

相关文章:

php - 无法连接到 Google 日历 PHP/API

mysql - 当我使用 --databases ='mydb' 指定时,innobackupex 始终备份 ibdata1

php - FreeBSD 上 PHP 的 MSSQL 连接

php - 如何在使用PHP跟踪错误$ e-> getTraceAsString()时隐藏密码

php - HTML 输入数组

mysql - WPF DataGrid 数据绑定(bind)到 MySql

javascript - 将 CSS 数据存储到 MySQL 数据库中

php - 有没有办法 var_dump 当前事件的无缓冲查询

php - 什么 PDO 函数等于 db::getOne

php - 如果 {} 在 if : endif