I get 2014 Cannot execute queries while other unbuffered queries are active when doing exec with PDO

梦想与她 提交于 2019-12-25 04:38:32

问题


I am doing a PDO::exec command on multiple updates:

$MyPdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
$MyPdo->exec("update t1 set f1=1;update t2 set f1=2");

I am doing it inside a transaction, and I keep getting the following error:

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.

those are the only query/ies


回答1:


You shouldn't execute the statements as a single query. Execute them one at a time:

$MyPdo->exec("update t1 set f1=1");
$MyPdo->exec("update t2 set f1=2");



回答2:


I encountered the same problem, and haven't found any solution:

$pdo->exec('update....;update...;')
$pdo->exec('update....;update...;')

Before php 5.3 you could close connection by destroy $pdo, in php 5.3+, you could use $pdo->query() to execute multi-query, take care that $pdo->exec() finished does not mean the sql server finished the execution:

$cnt=1000;
$sql='';
$j=0;
for ($i=0;$i<$cnt;++$i)
    {++$j;
     $sql.="update sem_UploadKeywordQueue set ProductCount='{$i}' where ID='{$i}';";
     if ($j==100)
        {$pdo=new PDO('mysql:host=domain.com;port=3306;dbname=db', "user", "pass");
         $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $pdo->exec($sql);
         $pdo=null;
         $sql='';
         $j=0;
        }
    }
if ($sql)
   {$pdo->exec($sql);
   }

for diyism



来源:https://stackoverflow.com/questions/1797690/i-get-2014-cannot-execute-queries-while-other-unbuffered-queries-are-active-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!