I need to run a bunch of queries in a script, but I get this error
PDOException(HY000): SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll().
My script
$db = Database::connection();
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `comments` (
...
) ENGINE=InnoDB AUTO_INCREMENT=86 DEFAULT CHARSET=utf8;
SQL;
$db->query($sql)->fetchAll();
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `comments_ratings` (
...
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SQL;
$db->query($sql)->fetchAll();
$sql = <<<SQL
CREATE TRIGGER ...
SQL;
$db->query($sql)->fetchAll();
$sql = <<<SQL
CREATE TRIGGER ...;
SQL;
$db->query($sql)->fetchAll();
My SQL syntax is valid, if I run the queries by hand they are successfully executed. As you can see I've added a call to fetchAll
after each query as the error message suggests, however I'm still seeing the same error. I found another answer that suggests calling closeCursor()
but if I swap the fetchAll
with closeCursor
the result is still the same. I also tried combining the SQL into 1 query but I'm getting a syntax error, it seems like I can't run multiple statements in 1 query.