0
votes

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. ) )

Code:

<?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));
}
?>

How can I fix it ?

1
Are remote connections enabled on the MSSQL server?DarkBee

1 Answers

0
votes
 $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 give all conncetion data in array. sql_connect contains only two parameters.