IM002 php config_error ai_generated partial

PDOException: SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified in /var/www/app/src/Database/OdbcConnector.php on line 21

ID: php/pdo-odbc-connection-string-malformed

Also available as: JSON · Markdown · 中文
87%Fix Rate
86%Confidence
1Evidence
2024-07-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
PHP 8.1.30 active
PHP 8.2.18 active
unixODBC 2.3.12 active

Root Cause

ODBC connection string references a DSN that is not defined in odbc.ini or /etc/odbc.ini, or the required ODBC driver is not installed/configured on the system.

generic

中文

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

Official Documentation

https://www.php.net/manual/en/ref.pdo-odbc.connection.php

Workarounds

  1. 90% success 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
    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. 85% success Use a full connection string without DSN: $pdo = new PDO('odbc:Driver={PostgreSQL Unicode};Server=localhost;Port=5432;Database=mydb;Uid=user;Pwd=pass;');
    Use a full connection string without DSN: $pdo = new PDO('odbc:Driver={PostgreSQL Unicode};Server=localhost;Port=5432;Database=mydb;Uid=user;Pwd=pass;');

中文步骤

  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;');

Dead Ends

Common approaches that don't work:

  1. 75% fail

    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.

  2. 60% fail

    Using a DSN name with spaces or special characters without quoting it in the connection string causes the driver manager to parse it incorrectly.