Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to connect to MS-SQL Server from PHP-8.0 (hosting on IIS-10 localhost) using SQL Server Driver for PHP, i.e., SQLSRV-5.9. While connecting from a PHP page, if I use SQL Server Authentication, it's getting connected right away. But while using Windows Authentication, it's throwing an error in the browser saying:

Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. ) )


My environment is Windows 10 Pro, Version 21H2, 64-bit. In my SQL Server, both Windows Authentication and SQL Server Authentication are enabled, and I'm able to connect to the server using both of them through SQL Server Management Studio. So what's stopping my PHP code to connect to the server using Windows Authentication? Both the PHP and SQL Sever are on the same PC. What am I doing wrong? As I'm new in PHP, can you please clarify it with a simple example?

Thanks and Regards!

What I have tried:

My connect-db.php code follows. Note how I used connection string for both Windows and SQL Server Authentication.

?php
    $serverName = "(local)";    // Optionally use port number (1433 by default).
    $connectionString = array("Database"=>"TestDB");    // Windows Authentication connection string.
    // $connectionString = array("UID"=>"sa","PWD"=>"sa","Database"=>"TestDB");    // SQL Server Authentication connection string.
    $conn = sqlsrv_connect($serverName, $connectionString); // Connect to the server.

    if($conn === false) {
        echo "Connection could not be established.<br/>";
        die(print_r(sqlsrv_errors(), true));
    } else {
        echo "Connection established successfuly.<br/>";
    }

    sqlsrv_close($conn);    // Close connection resources.
?>
Posted
Updated 12-Jan-22 6:28am
Comments
0x01AA 12-Jan-22 10:20am    
My Guess:
The credentials under which the Web server's process (or thread) is running must map to a valid SQL Server login in order to establish a connection.
Found here: How to: Connect Using Windows Authentication - PHP drivers for SQL Server | Microsoft Docs[^]
Maciej Los 12-Jan-22 12:13pm    
Bruno, it sounds like an answer.
0x01AA 12-Jan-22 12:26pm    
Thank you Maciej, even it is a wild guess ;)

1 solution

I would guess the problem is the account under which the web service runs.

See this...
Quote:
The credentials under which the Web server's process (or thread) is running must map to a valid SQL Server login in order to establish a connection.

... found here: How to: Connect Using Windows Authentication - PHP drivers for SQL Server | Microsoft Docs[^]

An easy test maybe would be if you start the web service under your user account (if that is possible and not ends in other problems).

I hope it helps.
 
Share this answer
 
v3
Comments
Maciej Los 12-Jan-22 12:42pm    
5ed!
0x01AA 12-Jan-22 13:06pm    
Thank you.
priyamtheone 13-Jan-22 10:25am    
@0x01AA: You go the root of the problem correctly, but the reference on the Microsoft site is too generalised for a beginner to understand. So it took me a while to get a grab on it through other references. The problem was with the NT AUTHORITY\IUSR not having any login in the SQL Server. Unlike .Net apps, IIS doesn't use the default Windows Authenticated login that comes with the SQL Server installation. It uses NT AUTHORITY\IUSR, which I was not aware of. But NT AUTHORITY\IUSR doesn't have any default login or DB user created for it in the SQL Server. You need to create them explicitly. Creating them and giving them respective privileges for your database solved the problem. Now I can login from PHP using SQL Server Authentication. Thanks for assisting. Appreciate your help.
0x01AA 13-Jan-22 11:25am    
You are very welcome. Thank you very much for your feedback.
Maybe you post your solution as _the_ solution for others fighting with the same. You will have for sure my 5 ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900