连接到 PHP 的 SQL 服务器:无法建立连接

Connect to SQL Server for PHP: Connection could not be established

Connection could not be established. Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 10061 [code] => 10061 [2] => [Microsoft][ODBC Driver 11 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it. [message] => [Microsoft][ODBC Driver 11 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired [message] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 10061 [code] => 10061 [2] => [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) )

代码:

<?php
$serverName = "localhost\sqlexpress, 1433"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"QLBH", "UID"=>"sa", "PWD"=>"123456");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if($conn) {
    echo "Connection established.<br />";
}
else{
    echo "<b>Connection could not be established.</b><br />";
    die( print_r( sqlsrv_errors(), true));
}
?>

我该如何解决?

 $serverName = "(local)";

$connectionOptions = array("Database"=>"dbname" Username=>Username,PWD=password);



/* Connect using Windows Authentication. */

$conn = sqlsrv_connect( $serverName, $connectionOptions);

if( $conn === false )

      { die( FormatErrors( sqlsrv_errors() ) ); }

connectionOptions 给出数组中的所有连接数据。 sql_connect 只包含两个参数。