# 致命错误: 未捕获的 PDOException: SQLSTATE[HY000]: 一般错误: 2014 无法在其他未缓冲查询活动时执行查询，位于 /var/www/app/src/Repository/UserRepository.php:22

- **ID:** `php/pdo-prepared-statement-column-count`
- **领域:** php
- **类别:** protocol_error
- **错误码:** `HY000/2014`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

PDO 的缓冲查询模式耗尽，因为上一个查询的结果集尚未完全获取或关闭，而同一连接上尝试了新查询，MySQL 禁止此操作。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 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 | — | — |

## 解决方案

1. ```
   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.
   ```
2. ```
   Close the cursor explicitly with $stmt->closeCursor() after fetching if not using fetchAll(), especially for unbuffered queries.
   ```
3. ```
   Use separate PDO connections for concurrent queries: $pdo1 = new PDO(...); $pdo2 = new PDO(...); to avoid interference.
   ```

## 无效尝试

- **** — 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. (70% 失败率)
- **** — Simply restarting the database server clears all connections temporarily, but the code will trigger the same error on the next request if not fixed. (90% 失败率)
