简体   繁体   English

为什么我不能使用 PHP 连接到我的 mssql 数据库?

[英]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.我做了很多研究,试图让我的 PHP 代码托管在 IIS 上,以连接到我的 MSSQL 数据库。 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连接失败 Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server ][SQL Server]用户“NT AUTHORITY\IUSR”登录失败。[消息] => [Microsoft][SQL 服务器的 ODBC 驱动程序 11][SQL Server]用户“NT AUTHORITY\IUSR”登录失败。)[1 ] => 数组 ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot打开登录请求的数据库“tttb_db”。登录失败。[消息] => [Microsoft][SQL 服务器的 ODBC 驱动程序 11][SQL Server]无法打开登录请求的数据库“tttb_db”。登录失败。) [2] => 数组 ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server ]用户“NT AUTHORITY\IUSR”登录失败。[消息] => [Microsoft][SQL 服务器的 ODBC 驱动程序 11][SQL Server]登录失败 user 'NT AUTHORITY\IUSR'.用户“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. ) ) ) [3] => 数组 ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][SQL 服务器的 ODBC 驱动程序 11][SQL服务器]无法打开登录请求的数据库“tttb_db”。登录失败。[消息] => [Microsoft][SQL 服务器的 ODBC 驱动程序 11][SQL Server]无法打开登录请求的数据库“tttb_db”。登录失败的。 ) )

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.我们的 SQL 服务器使用 Windows 身份验证,我们的服务器名称是AEGIS-PC\SQLEXPRESS ,用户名和密码块显示为灰色。 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. 1.)您的模块未加载,因为它的VC9代替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() . 2.)检查您的PHP版本并使用正确的PHP版本驱动程序,您可以在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. 3.)不要忘记安装MSSQL Native Client,否则无法连接到您的数据库,这是我每次都遇到的问题。

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. 您的代码看起来很好,如果您收到错误消息, sqlsrv_connect ,这是一个未加载模块的信号。

https://www.microsoft.com/en-us/download/details.aspx?id=20098 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. 您需要为“NT AUTHORITY \\ IUSR”系统帐户添加服务帐户,以允许IIS访问数据库。

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. 当有人点击网站并且php脚本试图访问SQL Server时,它使用IIS的系统帐户,而不是网站帐户上的用户。 You can have IIS use windows authentication for the DB request. 您可以让IIS对数据库请求使用Windows身份验证。 See below for enabling Windows Authentication with IIS: https://technet.microsoft.com/en-us/library/cc754628%28v=ws.10%29.aspx 有关使用IIS启用Windows身份验证的信息,请参见下文: 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. 公平警告,仅当用户点击网页与Web服务器位于同一AD域时才有效。

Be careful what privileges you assign to the IIS system account, if you go that route at all. 如果您完全走这条路线,请注意您为IIS系统帐户分配的权限。

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/ 结帐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服务/

it's just a few steps. 这只是几个步骤。

  1. Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\\NETWORK SERVICE and select Properties 转到SQL Server >>安全>>登录,右键单击NT AUTHORITY \\ NETWORK SERVICE并选择属性
  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. 在下部屏幕上,检查角色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. 1- Go 到 SQL 服务器 >> 安全 >> 登录并右键单击 NT AUTHORITY.NETWORK SERVICE 和 select 属性 2- 在新打开的登录属性屏幕中,go 到“用户映射”选项卡。 Then, on the “User Mapping” tab, select the desired database – especially the database for which this error message is displayed.然后,在“用户映射”选项卡上,select 所需的数据库——尤其是显示此错误消息的数据库。 On the lower screen, check the role db_owner.在下方屏幕上,检查角色 db_owner。 Click OK.单击确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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