致命错误: 未捕获的 PDOException: SQLSTATE[HY000]: 一般错误: 2014 无法在其他未缓冲查询活动时执行查询,位于 /var/www/app/src/Repository/UserRepository.php:22
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active in /var/www/app/src/Repository/UserRepository.php:22
ID: php/pdo-prepared-statement-column-count
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| PHP 7.4 | active | — | — | — |
| PHP 8.0 | active | — | — | — |
| PHP 8.1 | active | — | — | — |
| PHP 8.2 | active | — | — | — |
| PHP 8.3 | active | — | — | — |
| MySQL 5.7 | active | — | — | — |
| MySQL 8.0 | active | — | — | — |
| MariaDB 10.5 | active | — | — | — |
根因分析
PDO 的缓冲查询模式耗尽,因为上一个查询的结果集尚未完全获取或关闭,而同一连接上尝试了新查询,MySQL 禁止此操作。
English
PDO's buffered query mode is exhausted when a previous query's result set has not been fully fetched or closed, and a new query is attempted on the same connection, which MySQL forbids.
官方文档
https://www.php.net/manual/en/pdo.begintransaction.php解决方案
-
Ensure all previous result sets are fetched or closed before executing a new query: $stmt = $pdo->query('SELECT * FROM users'); $rows = $stmt->fetchAll(); // fetch all rows to free the result; then execute next query. -
Close the cursor explicitly with $stmt->closeCursor() after fetching if not using fetchAll(), especially for unbuffered queries.
-
Use separate PDO connections for concurrent queries: $pdo1 = new PDO(...); $pdo2 = new PDO(...); to avoid interference.
无效尝试
常见但无效的做法:
-
70% 失败
Setting PDO::MYSQL_ATTR_USE_BUFFERED_QUERY to false globally may cause memory issues and does not force fetching; it only switches to unbuffered mode which still requires closing.
-
90% 失败
Simply restarting the database server clears all connections temporarily, but the code will trigger the same error on the next request if not fixed.