如何在PHP中启用MS SQL驱动?(Windows7+PHP5.6.26+SQLServer2012)
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 -vin the command line or checking theArchitecturefield inphpinfo().
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 Safetyinphpinfo()—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_loginphpinfo()to find its location) for messages about failed extension loading. - Make sure the driver files in
C:\php\exthave 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

