Reputation: 71
I've done so much research trying to get my PHP code hosted on IIS to connect to my MSSQL database. I just can't seem to figure out the issue. Has anyone come across this before?
<?php
$serverName = 'AEGIS-PC\SQLEXPRESS';
$connectionInfo=array('Database'=>'tttb_db');
$con = sqlsrv_connect($serverName, $connectionInfo);
if($con){
echo 'Connection established<br />';
}
else {
echo 'Connection failed<br />';
die(print_r(sqlsrv_errors(), TRUE));
}
?>
Update, we have a new error:
Connection failed Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. ) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. ) [2] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. ) [3] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "tttb_db" requested by the login. The login failed. ) )
Our SQL server is using Windows Authentication and our server name is AEGIS-PC\SQLEXPRESS
with the user name and password blocks are greyed out. I can not think of reason our login would be failing. What are we doing wrong?
Upvotes: 7
Views: 12097
Reputation: 1
1- Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties 2- In newly opened screen of Login Properties, go to the “User Mapping” tab. Then, on the “User Mapping” tab, select the desired database – especially the database for which this error message is displayed. On the lower screen, check the role db_owner. Click OK.
Upvotes: 0
Reputation: 69
It's just a simple solution I found to mine. checkout http://blog.sqlauthority.com/2009/08/20/sql-server-fix-error-cannot-open-database-requested-by-the-login-the-login-failed-login-failed-for-user-nt-authoritynetwork-service/
it's just a few steps.
this should fix your problem
Upvotes: 3
Reputation: 27295
There are a few things that could cause such problems:
1.) Your modules aren't loaded because its VC9 instead if VC11. Check which compiler version your system use and install the correct driver.
2.) Check your PHP Version and use the correct driver for your PHP-Version your can check that in your phpinfo()
.
3.) Don't forget to install the MSSQL Native Client otherwise you can't connect to your database that is the problem what I have every time.
Your code looks good and if your get the error message that sqlsrv_connect
isn't found that is a signal that the module is not loaded.
https://www.microsoft.com/en-us/download/details.aspx?id=20098
Upvotes: 3
Reputation: 176
You need to add a service account for the 'NT AUTHORITY\IUSR' system account to let IIS access the database.
This is the key part of the error string:
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'
When someone hits the website and the php script tries to access the SQL Server, it does it using IIS's system account, not the user on the website's account. You can have IIS use windows authentication for the DB request. See below for enabling Windows Authentication with IIS: https://technet.microsoft.com/en-us/library/cc754628%28v=ws.10%29.aspx
Fair warning, it only works if the user hitting the web page is in the same AD domain as the web server.
Be careful what privileges you assign to the IIS system account, if you go that route at all.
Upvotes: 1