Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 = "SERV002SQLEXPRESS"; //serverNameinstanceName
    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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
211 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have the key/value pairs in the wrong order.

PHP's associative-array syntax is:

array( key => value )

So your code should be:

array( "Database" => "YSHDB", "UID" => "sa", "PWD" => "myPassword" );

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...