首页 > 解决方案 > 我有sql自定义执行方法,但是发现错误无缓冲查询

问题描述

我有一个具有自定义查询执行的类。如果在我执行之后它会显示一些错误。

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->closeCursor()输入我的代码,但我已经完成了它并且仍然得到错误。显示的错误与上面的错误相同。下面是我的 DBClassification 类。请帮我。谢谢 :)

<?php
class DBClassification
{
    public $db = null;
    public $host = "localhost";
    public $user = "root";
    private $pass = "";
    public $path = __DIR__ . "\\";
    public $prefixFilename = "klasifikasi_";
    public $dbexception = [];
    public $stmt;

    public function setHost($host){
        $this->host = $host;
    }

    public function setUser($user){
        $this->user = $user;
    }

    public function setPass($pass){
        $this->pass = $pass;
    }

    public function setPath($path)
    {
        if (!is_dir($path)) die ("Directory \$path is not correct!\n$path");

        $lastchar = substr($path, -1);
        if ($lastchar != "/" && $lastchar != "\\") $path = $path . "/";
        if (strpos($path, '/') !== false) $path = str_replace("/","\\",$path);

        $this->path = $path . $this->host . "\\"; // setting path to the generated output file
    }

    public function setPrefixFilename($prefixFilename){
        $this->prefixFilename = $prefixFilename;
    }

    public function setDBException($dbexception=[]){
        $this->dbexception = $dbexception;
    }

    public function init($host,$user,$pass,$path,$prefixFilename,$dbexception=[])
    {
        if (!$dbexception) $dbexception = ["information_schema","mysql","performance_schema","phpmyadmin","sys"];
        $this->setHost($host);
        $this->setUser($user);
        $this->setPass($pass);
        $this->setPath($path);
        $this->setPrefixFilename($prefixFilename);
        $this->setDBException($dbexception);
        $this->openConnection();
    }

    // Establishing Connection to mysql database on specified host
    public function openConnection(){
        try {
            $db = new PDO("mysql:host=".$this->host, $this->user, $this->pass);
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            $this->db = $db;
            return true;
        } catch (PDOException $e) {
            die("Error!: " . $e->getMessage());
        }
        return false;
    }
    public function run(){
        try {
            $databases = $this->stmtExec("SHOW DATABASES",true);
            foreach($databases as $database): // execute each database
                $dbname = $database['Database']; // database name
                if(!in_array($dbname,$this->dbexception)): // prevent clasifying dbname which contain in dbexception
                    echo "USE $dbname\n";
                    $this->stmtExec("USE $dbname");
                    $tables = $this->stmtExec("SHOW TABLES",true);

                endif; // if(!in_array($dbname,$dbexception)):
            endforeach; // foreach($databases as $database):
        } catch (Exception $e) {
            echo "Something wrong, failed to clasify each database in host " . $this->host . "\n";
            die("Error!: ".$e->getMessage());
        }
    }

    public function stmtExec($sql,$fetch=false)
    {
        try {
            $this->stmt = $this->db->prepare($sql);
            if ($fetch) {
                if ($this->stmt->execute()) $return = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
            } else {
                $return = $this->stmt->execute();
            }
            $this->stmt->closeCursor();
        } catch (PDOException $e) {
            $errormsg = "db Error: ". $this->stmt->queryString . PHP_EOL . $e->getMessage();
            $queryFilename = $this->path . "error.txt";
            file_put_contents($queryFilename, $errormsg);
            print_r($errormsg);die();
        }
        return $return;
    }
}

标签: phppdo

解决方案


[已回答]

我已经找到了解决方案,我必须删除 pdo 属性“PDO::ATTR_EMULATE_PREPARES”中的错误状态。我认为它会变成错误,因为它会在准备方法中检查查询。并且查询“use $dbname”是一个没有输出的查询,如果准备检查打开,则会出错。这就是我的全部意见。


推荐阅读