LoginSignup
1
0

More than 5 years have passed since last update.

Cannot execute queries while other unbuffered queries are active.

Posted at

事象

$pdo->query("drop database if exists DBNAME");
$pdo->query("create database DBNAME");
$pdo->query("use DBNAME");

みたいなことをしたら

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.

と言われてしまった。

原因

PDO::MYSQL_ATTR_USE_BUFFERED_QUERYはデフォルトでtrueになっているので、エラーメッセージが提示する修正は意味がなさそう。

よくわからないのでググったらstack overflowに書いてあった。
->query()->exec()にしたら良いらしい。

たぶん、query()PDOStatementを返すのでPDOStatement::closeCursor()が実行されるまで接続されっぱなしになるっぽい…?
PDO::MYSQL_ATTR_USE_BUFFERED_QUERYの力でわざわざ閉じなくても大丈夫なはずだと思ったけど、以下のようにqueryが二つだと動いたので三重入れ子になると厳しいっぽい。

$pdo->query("drop database if exists DBNAME");
$pdo->query("create database DBNAME");

対応

exec()PDOStatementじゃなくてintを返すので、接続が維持されないからアド。

$pdo->exec("drop database if exists DBNAME");
$pdo->exec("create database DBNAME");
$pdo->exec("use DBNAME");
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0