You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP 7.2连接SQL Server报错求助:已装ODBC驱动仍异常

Fixing SQL Server Connection Errors with PHP 7.2 on Windows Server

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.dll and pdo_sqlsrv72_ts.dll for Thread-Safe PHP, which is standard for IIS) exist in your PHP’s ext folder.
  • Refresh your phpinfo() page—look for sections labeled "sqlsrv" and "pdo_sqlsrv". If they’re missing, double-check that extension_dir in php.ini points to the correct ext directory.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:52:15