简体   繁体   中英

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

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.

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.

  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.

this should fix your problem

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM