我有sql自定义执行方法,但发现错误未缓冲查询

时间:2019-04-29 08:28:59

标签: php pdo

我有一个具有自定义查询执行功能的类。然后,如果我执行该命令,它将显示一些错误。

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

1 个答案:

答案 0 :(得分:0)

[回答]

我从中找到了解决方案,我必须删除pdo属性“ PDO :: ATTR_EMULATE_PREPARES”中的错误状态。我认为这将成为错误,因为它将在准备方法中检查查询。查询“ use $ dbname”是没有输出的查询,并且如果启用了prepare check,则将给出错误。那就是我的全部看法。