PHP 7.1连接SQL Server报错:建立连接时出现网络或实例相关错误
Hey there, let’s work through this connection problem together. Since you’ve already confirmed the SQL Server driver is properly loaded via phpinfo(), we can skip that check and focus on the most common causes for this "network-related or instance-specific error":
Step 1: Verify SQL Server Service is Running
- Open the Services app on Windows (press Win+R, type
services.msc). - Look for the service named
SQL Server (MSSQLSERVER)(or your specific instance name if using a named instance). - Make sure the service is in a Running state. If not, right-click and start it.
Step 2: Enable TCP/IP Protocol for SQL Server
Even for local connections, TCP/IP might be disabled by default:
- Open SQL Server Configuration Manager.
- Navigate to SQL Server Network Configuration > Protocols for [Your Instance Name].
- Right-click TCP/IP and select Enable.
- Double-click TCP/IP to open its properties, go to the IP Addresses tab:
- Scroll down to IPAll and ensure the TCP Port is set to
1433(the default port). - If using dynamic ports, note the port number and update your
$serverNameto include it (e.g.,tcp:localhost,12345).
- Scroll down to IPAll and ensure the TCP Port is set to
- Restart the SQL Server service to apply changes.
Step 3: Validate Connection Parameters
Double-check these critical values in your code:
- Server Name: For the default SQL Server instance, simplify this to
"localhost"or".". For a named instance, use the format"localhost\INSTANCE_NAME"(e.g.,"localhost\SQLEXPRESS"). - Credentials: Confirm the
saaccount is enabled (it’s disabled by default in some SQL Server versions) and the password"Aa123456"is correct. Also, ensure thesaaccount has permissions to access thefcdatabase. - Database Name: Make sure the
fcdatabase exists on your SQL Server instance (verify in SQL Server Management Studio).
Step 4: Test Port Connectivity
Check if port 1433 is accessible:
- Use PowerShell: Run
Test-NetConnection localhost -Port 1433. If the result showsTcpTestSucceeded: True, the port is open. - If blocked, check Windows Firewall settings and add an inbound rule allowing traffic on port 1433 for SQL Server.
Step 5: Check SQL Server Authentication Mode
The sa account requires mixed-mode authentication to work:
- Open SQL Server Management Studio and connect to your instance.
- Right-click the server instance > Properties > Security.
- Select SQL Server and Windows Authentication mode.
- Click OK and restart the SQL Server service.
Improved Code for Better Error Debugging
Replace your current code with this version to get detailed error context, which will help narrow down the issue:
try { $serverName = "localhost"; // Adjust to your instance name if needed $connectionOptions = array( "Database" => "fc", "Uid" => "sa", "PWD" => "Aa123456", "ReturnDatesAsStrings" => true // Optional: avoids date type conversion issues ); $conn = sqlsrv_connect($serverName, $connectionOptions); if ($conn === false){ $errors = sqlsrv_errors(); foreach ($errors as $error) { echo "SQLSTATE: " . $error['SQLSTATE'] . "<br>"; echo "Error Code: " . $error['code'] . "<br>"; echo "Error Message: " . $error['message'] . "<br>"; } die(); } else { echo "Successfully connected to the database!"; } } catch (Exception $e) { echo("Exception caught: " . $e->getMessage()); }
If you’re still stuck after trying these steps, share the detailed error output from this code—it’ll help pinpoint exactly what’s failing (e.g., login rejection, instance not found, etc.).
内容的提问来源于stack exchange,提问作者Mcnoon

