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?
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
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.
Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties
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.
this should fix your problem
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.
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.
Related
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?
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
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.
Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties
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.
this should fix your problem
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.
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.
The official guide for connecting to SQL Server using the PHP driver from Microsoft seems pretty straightforward, so I copied the text for the most part:
$server = "serverlocation\prod";
$database = array( "Database"=>"temp");
$conn = sqlsrv_connect($server, $database);
echo "<pre>";
if ( $conn ) {
echo "Connection established";
} else {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
When I run the code, I got the following error:
Connection could not be established.
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 '*****DOMAIN\MY_MACHINE_NAME*****'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user '*****DOMAIN\MY_MACHINE_NAME*****'.
)
I am running this code on the machine name that is listed in the error message, which is a Windows machine, but the machine name listed is NOT the user account under which I am logged in. The user account under which I am logged in has rights to this database, (and I can connect to it using MS SQL Server Managment Studio just fine...) but when I try to connect using this script, it displays my machine name instead of my user account.
What am I doing wrong?
Assuming that you're running this over the web:
You should be using IIS and not Apache†
IIS must have Windows Authentication enabled
Anonymous authentication must be disabled
You must set fastcgi.impersonate = 1 in your php.ini
Assuming that the account you're logged in as gets recognized by SQL Server, you should be good to go. See this page for a complete discussion.
†In the comments for that post, the author suggests that this may also be possible with Apache.
I'm connecting SQL Database to a PHP website (Both are on Azure),
Everything in PHP seems to be fine, and Connection String is also Perfect.
When I try to connect to a database it connects and does all the operations, but when I try to connect to another database it show the following error.
Array (
[0] => Array (
[0] => 08001
[SQLSTATE] => 08001
[1] => 5
[code] => 5
[2] => [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [5].
[message] => [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [5].
)
[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] => 5
[code] => 5
[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.
)
)
And also,
I'm seeing this Lock sign on the database which is not getting connected.
This Lock doesn't appear on the database, which connects successfully.
And also All three (Two Databases and a Web App) are on Same Resource Group.
I'm connecting both database using Connection String stored on website's Application Settings.
Actually, this is TDE (Transparent Data Encryption) icon rather than a Lock sign. See Transparent Data Encryption with Azure SQL Database.
And according to the error message above, the most likely cause of this issue is that the Server name you are using is incorrect. Here are some proposals for you to troubleshoot this issue:
Make sure the hostname from Connection String exactly equals to the Server name in the Azure portal.
Use the sample code for PHP to test whether the connection could be established or not. Do not forget to replace with your own password.
I have WAMP and am trying to connect to to my SQL Server 2008 database by entering a domain user and password using the following PHP command:
$serverName = "MySQLServer\Instance"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName",
"UID"=>"myADUserName", "PWD"=>"MyPassword");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
As I understand it, it is trying to authenticate with a SQL Server created user and not from the Active Directory, so I am getting the following error message:
Array ( [0] =>
Array ( [0] => 28000 [SQLSTATE] => 28000
[1] => 18456 [code] => 18456
[2] => [Microsoft][SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
[message] => [Microsoft]
[SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
)
[1] =>
Array ( [0] => 28000 [SQLSTATE] => 28000
[1] => 18456 [code] => 18456
[2] => [Microsoft][SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
[message] => [Microsoft]
[SQL Server Native Client 10.0]
[SQL Server]Login failed for user 'myADUserName'.
)
)
Is this possible in PHP with WAMP?
I've read about changing the log on account in the wampapache service to myADUserName, but the user doesn't have access administrator access on the server to allow this and I wouldn't want to tie one account to all my PHP applications.
When you assign "UID"=>"myADUserName", "PWD"=>"MyPassword" you are using sql-authentication and not Windows authentication.
But you can create that user/pwd combination into the sql-server as sql-login -> SQL Server Authentication.
When you are want to use windows authentication you don't need to specify uid and pwd. the authentication is taken from the running apache process. Of curse that one needs to be run into that windows account.
See Microsoft documentation
Remarks If values for the UID and PWD keys are not specified in the
optional $connectionInfo parameter, the connection will be attempted
using Windows Authentication. For more information about connecting to
the server, see How to: Connect Using Windows Authentication and How
to: Connect Using SQL Server Authentication.
It looks strange, but this one worked for me,
$dbName='dB-Name';
$dbUser='User1';
$dbPass='B4X345ZXsaoi0omkLH';
$connectionInfo = array( "Database"=>$dbName, "UID"=>$dbUser, "PWD"=>$dbPass);
If you are using SQLSERVER this wont work with php 5.5(I'm using this version didn't test any other)
$connectionInfo = array( "Database"=>"DBName", "UID"=>"User1", "PWD"=>"B4X345ZXsaoi0kLH");
I know this is not a new question because i found lots of similar questions but not the one which solves my issue.
Well my question is how do i connect to a remote sql 2000 server using other domain windows credentials (not the one im logged in with) with php running on apache (xampp)... and the server doesnt run named instance.
These the things i tried,
installed SQL Native client for my php 5.3
got the .dll files to the ext folder
made sure i got the sqlsrv up and running via phpinfo
mssql.secureconnections is on
edited the php.ini and restart the apache server
So, i tried looking at various posts here and elsewhere but couldnt actually solve my problem.
i tried several codes, but here is the one which im currently trying with
$server = "XXX_Server";
$connInfo = array( "Database"=>"DB_Server", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect($server, $connInfo);
if( $conn )
{
echo "Connection established.";
}
else
{
echo "Connection could not be established.";
die( print_r( sqlsrv_errors(), true));
}
and i get error as shown below,
Connection could not be established.
Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'xx\username'. ) )
where xx = domain
And please note that , the client has given me a windows credentials which has access to the server and must use only this. And when i hardcoded my credentials (which has access too) in the script, it didnt work and got the error above, but when i use QueryExpress / SQL Studio, im able to access the db using windows credentials (mine).
So to make it simple, i need to write a script where i need to hardcode the windows credentials and connect to a SQL DB (Stuck here) and make the script accessible for only few using windows ad authentication (which i know how to do)
Please provide me some tips and advice as im stuck and dont know how to proceed. Whatever i research and try, i end up doing the same thing, facing a wall :P
Thanks in advance