干嘛我一用自带的PDO就报错啊(已解决,请老板看下是不是源码问题)。。

chenyeyu

已经解决了,我在把GatewayWorker\Lib\DbConnection.php 的$sQuery属性改成 public,然后在我的业务代码执行完single()查询之后,调用一下$db1->sQuery->closeCursor();就可以了,代码如下。。

 $orderCount = $db1->single("select count(*) as count from t2 limit 1;");
 $db1->sQuery->closeCursor();
    $callCount = $db1->single("select count(*) as count from t1 limit 1;");
    $db1->sQuery->closeCursor();

我写了个定时器,就是去查询订单总数,然后推送给指定的客户端。他第一次查询就没问题,第二次查询就会报错,然后是有时候没问题,有时候报错
我试了下在这个function single()函数renturn的前面加了句代码

$this->sQuery->closeCursor();
return $this->sQuery->fetchColumn();

他就不报错,但是返回来的都是flase了。。
看那报错的代码是不是在说还有查询在执行的意思啊。。
这是我的定时器代码

 \Workerman\Lib\Timer::add(10, function() {
            $config_name = 'XXXX';
            $gate = new \GatewayWorker\Lib\Gateway();
            $db = new \GatewayWorker\Lib\Db();

            $tempClient = array();
            $allSession = $gate->getAllClientSessions();
            foreach ($allSession as $key => $value) {
                if (isset($value) && $value == $config_name) {
                    $tempClient[] = $key;
                }
            }
            //如果存在是XXXX的客户端
            if (count($tempClient) > 0) {
                $config = Config\Db::$$config_name;
                $db1 = $db->instance($config_name);
                $count = $db1->single("select count(*) as count from {$config}order  limit 1;");
                foreach ($tempClient as $value) {
                    $gate->sendToClient($value, json_encode(array( 'order_count' => $count)));
                }
            }
        });

这是报错代码

client:180.136.150.226:60103 gateway:127.0.0.1:7272  client_id:7f0000010b5600000001 session:{"orign":"laikediancan"} onMessage:{"orign":""}
PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE: 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.' in /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php:1724
Stack trace:
#0 /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php(1724): PDO->prepare('select count(*)...')
#1 /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php(1906): GatewayWorker\Lib\DbConnection->execute('select count(*)...', NULL)
#2 /home/wwwroot/diancan/public_html/im/GatewayWorker/Applications/IM/start_businessworker.php(52): GatewayWorker\Lib\DbConnection->single('select count(*)...')
#3 : {closure}()
#4 /home/wwwroot/diancan/public_html/im/GatewayWorker/Workerman/Event in /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php on line 1724

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE: 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.' in /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php:1724
Stack trace:
#0 /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php(1724): PDO->prepare('select count(*)...')
#1 /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php(1906): GatewayWorker\Lib\DbConnection->execute('select count(*)...', NULL)
#2 /home/wwwroot/diancan/public_html/im/GatewayWorker/Applications/IM/start_businessworker.php(52): GatewayWorker\Lib\DbConnection->single('select count(*)...')
#3 : {closure}()
#4 /home/wwwroot/diancan/public_html/im/GatewayWorker/Workerman/Event in /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php on line 1724
WORKER EXIT UNEXPECTED E_ERROR Uncaught exception 'PDOException' with message 'SQLSTATE: 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.' in /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php:1724
Stack trace:
#0 /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php(1724): PDO->prepare('select count(*)...')
#1 /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php(1906): GatewayWorker\Lib\DbConnection->execute('select count(*)...', NULL)
#2 /home/wwwroot/diancan/public_html/im/GatewayWorker/Applications/IM/start_businessworker.php(52): GatewayWorker\Lib\DbConnection->single('select count(*)...')
#3 : {closure}()
#4 /home/wwwroot/diancan/public_html/im/GatewayWorker/Workerman/Event in /home/wwwroot/diancan/public_html/im/GatewayWorker/GatewayWorker/Lib/DbConnection.php on line 1724
worker exit with status 65280
5635 1 0
1个回答

walkor

报错的意思是返回的结果集是多行,你只取了1行或者说没有取完,然后又执行查询,就会报错。

可能你哪里调用了single 或者row sql没指定limit 1

年代过于久远,无法发表回答
🔝