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

如何在PHP中启用MS SQL驱动?(Windows7+PHP5.6.26+SQLServer2012)

Fixing SQLSRV Driver Installation Issues for PHP 5.6 on Windows 7 + SQL Server 2012

Hey there, let's walk through getting that SQL Server driver working for your PHP 5.6 setup—this is a common gotcha when switching from MySQL, so let's break down the steps to troubleshoot and fix it:

First, Double-Check Driver Compatibility

You mentioned using SQLSRV32.EXE, but make sure this matches your PHP architecture:

  • If your PHP is 32-bit, SQLSRV32 is correct.
  • If your PHP is 64-bit, you'll need the 64-bit version of the SQLSRV driver package instead. You can confirm your PHP architecture by running php -v in the command line or checking the Architecture field in phpinfo().

Verify the Extension Directory Path

Before adding the driver line, make sure your php.ini has the correct extension_dir set. Look for this line:

extension_dir = "C:\php\ext"

If it's commented out (starts with ;) or pointing to a different folder, uncomment it and set it to your C:\php\ext path.

Add the Correct Driver Line to php.ini

You said you started adding an extension line—let's make sure it's the right one. The SQLSRV32 package includes two versions for PHP 5.6:

  • For Thread-Safe (TS) PHP (check Thread Safety in phpinfo()—it'll say "Enabled"):
    extension=php_sqlsrv_56_ts.dll
    
  • For Non-Thread-Safe (NTS) PHP (Thread Safety says "Disabled"):
    extension=php_sqlsrv_56_nts.dll
    

Don't forget to save php.ini after making this change!

Install the Required ODBC Driver

The SQLSRV driver depends on the ODBC Driver 11 for SQL Server—this is a critical step many people miss. Download and install the version matching your system architecture (32/64-bit) on your Windows 7 machine.

Restart Your Web Server

After making these changes, restart your web server (IIS, Apache, etc.)—PHP won't pick up new extensions until you do this.

Test the Connection

Create a simple test script to verify the driver is working:

<?php
// Replace with your SQL Server details
$serverName = "localhost"; // or your server IP/name
$connectionInfo = [
    "Database" => "your_database_name",
    "UID" => "your_sql_username",
    "PWD" => "your_sql_password"
];
$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn) {
    echo "Successfully connected to SQL Server!";
} else {
    echo "Connection failed. Error details:<br>";
    print_r(sqlsrv_errors(), true);
}
?>

Run this script in your browser—if it connects, you're good to go! If not, check the error output for clues (like missing ODBC driver, wrong credentials, or driver architecture mismatch).

Troubleshooting Extra Tips

  • Check PHP's error log (look for error_log in phpinfo() to find its location) for messages about failed extension loading.
  • Make sure the driver files in C:\php\ext have read permissions for the user running your web server.
  • If connecting to a remote SQL Server 2012 instance, ensure the server allows remote connections and the firewall isn't blocking port 1433.

内容的提问来源于stack exchange,提问作者sab669

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:16:39