没有SELECT:当其他无缓冲的查询处于活动状态时无法执行查询(INSERT INTO / ANALYZE TABLE / BEGIN TRANSACTION)

I got the error: 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. But:

  1. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY is ON
  2. I don't use pdo's query() just exec().

I think there is a Problem with "ANALYZE TABLE". It returns a resultset with on row. I don't need this information. I can't change the order of the sql commands because in real that a different batches that a called in the night by a cron job...

A test code:

pdo()->exec("TRUNCATE TABLE test;");
pdo()->exec("INSERT INTO test SELECT id FROM data");
//    pdo()->exec("INSERT INTO test VALUES (1)"); if only VALUES(..) is used no error thrown
pdo()->exec("ANALYZE TABLE test"); // this returns 1 row
pdo()->beginTransaction(); // here comes the exception!

The connection settings:

PDO::ATTR_EMULATE_PREPARES => 1
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => 1
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8, lc_time_names = 'de_DE',sql_mode ='STRICT_ALL_TABLES,PIPES_AS_CONCAT', group_concat_max_len=1000000" ; 
PDO::MYSQL_ATTR_COMPRESS => 1

My actual Workarround is:

$cmd = pdo()->query("ANALYZE TABLE test");
$cmd->closeCursor();
unset($cmd);

Is there any missconfiguration or is this a bug in 10.2.22-MariaDB-log or PHP Version 7.2.10?

I don't use pdo's query() just exec().

THIS is your problem exactly. Mysql has no idea whether you need the result or not. Or, rather, it assumes that as long as you made a request, the result it will be waiting for you. Hence, using exec() for a query that returns any data just makes no sense and also is the exact reason why you are getting the error.