PHP 7.2连接SQL Server报错求助:已装ODBC驱动仍异常
Let’s work through this step by step—those ODBC driver headaches are super common, especially with PHP 7.2 on Windows Server. Here’s what to check to get your connection working:
1. Make Sure Architecture Matches (x64 vs x86)
This is the #1 mistake people make. Even if you downloaded the x64 ODBC driver, you need to confirm your PHP installation uses the same architecture:
- If your PHP is 32-bit, you need the 32-bit ODBC driver, and configure it via the 32-bit ODBC manager at
C:\Windows\SysWOW64\odbcad32.exe - For 64-bit PHP, stick with the x64 driver and use
C:\Windows\System32\odbcad32.exe
To check PHP’s architecture, create a quick phpinfo.php file:
<?php phpinfo(); ?>
Run it in your browser and look for the "Architecture" line (it’ll say x86 or x64).
2. Verify PHP Extensions Are Actually Loaded
You added the extensions to php.ini, but let’s confirm they’re being picked up:
- In
php.ini, remove any leading semicolons from these lines:extension=sqlsrv extension=pdo_sqlsrv - Check that the extension files (like
sqlsrv72_ts.dllandpdo_sqlsrv72_ts.dllfor Thread-Safe PHP, which is standard for IIS) exist in your PHP’sextfolder. - Refresh your
phpinfo()page—look for sections labeled "sqlsrv" and "pdo_sqlsrv". If they’re missing, double-check thatextension_dirinphp.inipoints to the correctextdirectory.
3. Fix the "Data Source Name Not Found" Error
This usually comes from a misconfigured DSN, or relying on a DSN when a DSN-less connection is more reliable:
- Option 1: Use a DSN-less connection string (skip setting up a DSN entirely)
Try this code instead:$serverName = "your_sql_server_name_or_ip"; $connectionOptions = [ "Database" => "your_database_name", "Uid" => "your_sql_username", "PWD" => "your_sql_password" ]; $conn = sqlsrv_connect($serverName, $connectionOptions); if ($conn === false) { die(print_r(sqlsrv_errors(), true)); } - Option 2: Validate your DSN
If you want to keep using a DSN, open the correct ODBC manager (matching your PHP architecture), go to the "System DSN" tab, and confirm your DSN is listed with the right driver (ODBC Driver 13 or 17 for SQL Server works best with PHP 7.2).
4. Confirm ODBC Driver Compatibility
PHP 7.2 doesn’t play nicely with the latest ODBC drivers (like version 18). Uninstall your current driver and install either ODBC Driver 13 or 17 for SQL Server—these are explicitly supported for PHP 7.2.
5. Check IIS Application Pool Permissions
If you’re using Windows Authentication to connect to SQL Server, make sure your IIS app pool’s identity has access to the SQL instance:
- Open IIS Manager, go to your app pool, and check the "Identity" under "Advanced Settings"
- Add that user (or group) as a login in SQL Server with the necessary database permissions.
After making any changes, restart IIS one more time to ensure all settings take effect.
内容的提问来源于stack exchange,提问作者Vitor Hallais

