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

PHP 7.1连接SQL Server报错:建立连接时出现网络或实例相关错误

Troubleshooting PHP 7.1 SQL Server Connection Issues

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:

  1. Open SQL Server Configuration Manager.
  2. Navigate to SQL Server Network Configuration > Protocols for [Your Instance Name].
  3. Right-click TCP/IP and select Enable.
  4. 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 $serverName to include it (e.g., tcp:localhost,12345).
  5. 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 sa account is enabled (it’s disabled by default in some SQL Server versions) and the password "Aa123456" is correct. Also, ensure the sa account has permissions to access the fc database.
  • Database Name: Make sure the fc database 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 shows TcpTestSucceeded: 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:

  1. Open SQL Server Management Studio and connect to your instance.
  2. Right-click the server instance > Properties > Security.
  3. Select SQL Server and Windows Authentication mode.
  4. 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:32:46