PHP: Connect to MS SQL Server using Domain user and Password - php

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");

Related

PHP connection with SQL Server using sqlsrv_connect [duplicate]

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.

Seeing Lock on SQL Database - Azure

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.

Why can't I connect to my mssql database using PHP?

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.

Connecting to MS SQL Server 2000 using PHP with Windows Credentials

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

Connecting to SQL Server 2008 through PHP

I need to connect to a SQL Server 2008 through PHP (WAMP, latest version). I have the sqlsrv drivers installed and set up and they do show up in phpinfo().
I'm using the following lines to connect to my DB, using Windows Authentication:
$serverName = "(local)";
$connectionOptions = array("Database"=>"MyTestDatabase");
$conn = sqlsrv_connect( $serverName, $connectionOptions) or die("Error!");
And I'm getting the following error:
Array
(
[0] => Array
(
[0] => IMSSP
[SQLSTATE] => IMSSP
[1] => -49
[code] => -49
[2] => This extension requires the Microsoft SQL Server 2011 Native Client. Access the following URL to download the Microsoft SQL Server 2011 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712
[message] => This extension requires the Microsoft SQL Server 2011 Native Client. Access the following URL to download the Microsoft SQL Server 2011 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712
)
[1] => Array
(
[0] => IM002
[SQLSTATE] => IM002
[1] => 0
[code] => 0
[2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
[message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
)
)
Any help would be great, but please be specific since I really don't know my way around WAMP except for a few basics.
The error is caused by a permission issue in the registry. There is a key where the path to the native client is stored and the identity you are using in the application pool is getting denied access.
Download Process Monitor and follow the instructions on this post:
http://www.iislogs.com/articles/processmonitorw3wp/ (This tutorial shows how to do it on IIS, with WAMP you will need to find the .exe process that runs on memory)
Find the registry key where the process w3wp.exe is being denied access. That in the case of IIS, not sure what's the name of the process in WAMP but the procedure is the same.
In my case:
HKLM\Software\ODBC\ODBCINST.INI\SQL Native Client 10.0
Run regedit and find the key
Right click and go to Permissions
Add Network Service to the list and give it Read permissions.
Recycle the app pool and it should work
sqlsrv_connect PHP manual
Try this approach to see what the error actually is:
$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));
}
Based on your setup, you may need to revert to the mssql_connect and corresponding functions. This works fine with mssql 2008 and you don't usually lose meaningful functionality.
Here's an example of setting up a connection to the database. Depending on your configuration you may have to add port information, etc.
$hostname = "server.domain.com";
$database = "DatabaseName";
$username = "LocalSQLUserName";
$password = "P#ssw0rd";
$Connection = mssql_connect($hostname, $username, $password);
If you are running on a XAMP environment, then it would stand to reason that you are running everything locally. If that's the case, your server would be localhost or 127.0.0.1 and you can define your own accounts in SQL. As I said, I don't use Windows accounts myself, but you can add an NT account to your "server" and then in SQL Server Management tool set that user to have access to the DB you are using. Then you have set the account and password and know what they are. If you have trouble with the user account, you can try computerName\userName.
If you are not the admin, you'll need to get the relevant information from them.

Categories