douhao5280 2019-04-29 08:28
浏览 42

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

i have a class which has custom query execution. Then it shown some error if after i execute that.

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.

I have search out my problem answer but, almost of them recommend me to put $stmt->closeCursor() in my code, but i have done with it and still get error. The error shown is same with error above. Below is my DBClassification class. Please help me. Thanks :)

<?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!
$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
";
                    $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 . "
";
            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条回答 默认 最新

  • dou4064 2019-04-29 10:09
    关注

    [ANSWERED]

    I have found the solution from that, i have to delete false status in pdo attribute "PDO::ATTR_EMULATE_PREPARES". I think it become error because it will check query in prepare method. And query "use $dbname" is one which has no output and will give error if prepare check is on. Thats all my opinion.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法