I am trying to make a connection to a Microsoft SQL Server 2008 Express database and I get the following error:
Connection could not be established. Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -1 [code] => -1 [2] => Invalid option YSHSDB was passed to sqlsrv_connect. [message] => Invalid option YSHSDB was passed to sqlsrv_connect. ) )
I was following this http://php.net/manual/en/function.sqlsrv-connect.php
PHP CODE:
<?php
$serverName = "SERV002\SQLEXPRESS"; //serverName\instanceName
array( "Database" => "YSHDB", "UID" => "sa", "PWD" => "myPassword" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if ( $conn ) {
echo "Connection established.<br />";
} else {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
EDIT:
I have fixed that line now i get this: "Connection could not be established.
Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => -1 [code] => -1 [2] => [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. [message] => [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. ) [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] => -1 [code] => -1 [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. )
I have checked to see if the server allows remote connections and it does
You have the key/value pairs in the wrong order.
PHP's associative-array syntax is:
So your code should be:
I do note that using the
sa
account to connect to SQL Server is generally bad practice, instead use Windows Authentication (using the Application Pool identity or current authentication scheme's user in IIS) or at least a per-application custom SQL Server Login user account with a minimal set of privileges granted.As for an editorial: SQL Server 2008 is now 7 years out of date (that's like running Windows NT 4 in 2005), please consider upgrading, not least for security reasons.
In my case the problem was for instance name. If you use default instance name, you don't need to set the instance name.Just set server name.This is my code:
you can learn more here:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/8b07a787-d2da-4336-b422-2e8199052d5e/sql-server-connection-timing-out?forum=sqldriverforphp