John Doesoph
John Doesoph

Reputation: 13

PHP sqlsrv_connect Error

I'm trying to connect to a MS SQL Server with PHP and failing. I'm running PHP7.2 and I've installed Microsoft Drivers 5.2 for PHP for SQL Server and Microsoft ODBC Driver 17 for SQL Server.

Here is the code I'm using...

$serverName = "severname\MSSQLSERVER";

$conn_array = array (
    "UID" => "username",
    "PWD" => "password",
    "Database" => "databasename",
);
$conn = sqlsrv_connect($serverName, $conn_array);
if ($conn){
    echo "connected";
}else{
    die(print_r(sqlsrv_errors(), true));
}

and here is the error message I'm getting...

> Array ( 
>         [0] => Array ( 
>             [0] => 08001 
>             [SQLSTATE] => 08001 
>             [1] => 87 
>             [code] => 87 
>             [2] => [Microsoft][ODBC Driver 17 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87].    
>             [message] => [Microsoft][ODBC Driver 17 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. 
>         ) [1] => Array ( 
>             [0] => HYT00 [SQLSTATE] => HYT00 
>             [1] => 0 [code] => 0 
>             [2] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired 
>             [message] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired 
>         ) [2] => Array ( 
>             [0] => 08001 
>             [SQLSTATE] => 08001 
>             [1] => 87 [code] => 87 
>             [2] => [Microsoft][ODBC Driver 17 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 17 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.
>         )
>     )

Update: After removing the instance name I get this error... SQLSTATE[28000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'USERNAME'.

Upvotes: 1

Views: 6770

Answers (1)

squillman
squillman

Reputation: 13641

As @ficuscr hints at, most likely your problem is the instance name. MSSQLSERVER is the instance name of the default instance which you do not refer to using the server\instance format. You just use server. You only have to use server\instance format for named instances.

Assuming this is the case, your first line should be

$serverName = "severname";

Upvotes: 2

Related Questions