【问题标题】:PHP5 MySql and PDO ExceptionPHP5 MySql 和 PDO 异常
【发布时间】:2012-09-25 00:17:52
【问题描述】:

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

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

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

这是错误:

SQLSTATE[HY000]:一般错误:2014 在其他无缓冲查询处于活动状态时无法执行查询。考虑使用 PDOStatement::fetchAll()。或者,如果您的代码只针对 mysql 运行,您可以通过设置 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY 属性来启用查询缓冲。

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

第一部分:

        $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]:一般错误:2006 MySQL 服务器已消失

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

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

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

期待找出我在此过程中遗漏的原因。希望你能指出我正确的方向。

【问题讨论】:

  • @Erik,我没有转义 PDO 我正在使用 php 命名空间并且斜杠是必需的,因此 php 不会在我的班级中查找 PDO。
  • 酷!你每天都会学到新东西:)

标签: php mysql pdo


【解决方案1】:

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

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

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

【讨论】:

  • 请注意,我将 unset($stmt) 直接放在 $rows = fetchAll() 之后,因为错误是在单独的类中引发的,因此此时 $stmt 在新范围内。
猜你喜欢
  • 2012-11-03
  • 1970-01-01
  • 2016-01-19
  • 2010-10-02
  • 2014-05-29
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多