Skip to content
Advertisement

Connection Could Not be Established with SQL Server 2019

I’m having a problem running my PHP project on our IIS with SQL Server 2019. My version of PHP is 7.0.26 and I have already imported SQLSRV DLLS.

When I try to login with my system, the following error is popping up:

Connection could not be established. Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user ‘sa’. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user ‘sa’. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user ‘sa’. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user ‘sa’. ) )

This is some part of my connection string.

$myCon = array("Database"=>$myDB, "UID"=>$myUser, "PWD"=>$myPass);
$db = sqlsrv_connect( $myServer, $myCon);

Also, I tried to trace the error, like adding the wrong credentials to see if there is some changes on the error message but it is still the same.

We tried some configurations, like setting up the server authentication into “SQL Server and Windows Authentication Mode”, firewall is disabled, and also setting up the Native client configuration.

IIS configuration was made setting MIME Type, default document, connection string, Application Pool, and all the path config was set.

I also installed only ODBC Driver 13, 2015 Redistributable, .NET 4 framework.

Can anyone help me? Thank you.

Advertisement

Answer

The SA account is created during the installation process. The SA account is well known and often targeted by malicious users, so it is advisable to disable the sa account unless your application requires it. you could create a new account in the SQL server or use windows authentication for accessing the SQL database.

<?php
$serverName = "serverName\sqlexpress"; //serverNameinstanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$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));
}
?>

Refer below link for more detail on how to create a user and configure with iis:

https://forums.iis.net/post/2159167.aspx

https://docs.microsoft.com/en-us/sql/connect/php/programming-guide-for-php-sql-driver?view=sql-server-ver15

https://docs.microsoft.com/en-us/sql/connect/php/connecting-to-the-server?view=sql-server-ver15

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement