# PDOException：SQLSTATE[IM002] SQLDriverConnect：0 [unixODBC][驱动程序管理器]未找到数据源名称，且未指定默认驱动程序，位于 /var/www/app/src/Database/OdbcConnector.php 第 21 行

- **ID:** `php/pdo-odbc-connection-string-malformed`
- **领域:** php
- **类别:** config_error
- **错误码:** `IM002`
- **验证级别:** ai_generated
- **修复率:** 87%

## 根因

ODBC 连接字符串引用了未在 odbc.ini 或 /etc/odbc.ini 中定义的 DSN，或者系统上未安装/配置所需的 ODBC 驱动程序。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| PHP 8.1.30 | active | — | — |
| PHP 8.2.18 | active | — | — |
| unixODBC 2.3.12 | active | — | — |

## 解决方案

1. ```
   Verify DSN configuration: odbcinst -j shows config files; check /etc/odbc.ini for [MyDSN] section with Driver= and Server= entries. Add missing DSN: echo -e '[MyDSN]\nDriver=PostgreSQL Unicode\nServer=localhost\nPort=5432\nDatabase=mydb' >> /etc/odbc.ini
   ```
2. ```
   Use a full connection string without DSN: $pdo = new PDO('odbc:Driver={PostgreSQL Unicode};Server=localhost;Port=5432;Database=mydb;Uid=user;Pwd=pass;');
   ```

## 无效尝试

- **** — Installing only the ODBC driver (e.g., psqlodbc for PostgreSQL) without configuring the DSN in odbc.ini still results in the same error because the driver is not associated with a DSN name. (75% 失败率)
- **** — Using a DSN name with spaces or special characters without quoting it in the connection string causes the driver manager to parse it incorrectly. (60% 失败率)
