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

问题描述:

我做了很多研究,试图让我的 PHP 代码托管在 IIS 上以连接到我的 MSSQL 数据库.我似乎无法弄清楚这个问题.有没有人遇到过这个?

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:

连接失败数组 ( [0] => 数组 ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [代码] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]用户NT AUTHORITY\IUSR"登录失败.[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]用户NT AUTHORITY\IUSR"登录失败.) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]无法打开数据库登录请求tttb_db".登录失败.[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]无法打开登录请求的数据库tttb_db".登录失败.) [2] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]登录用户NT AUTHORITY\IUSR"失败.[消息] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]用户NT AUTHORITY\IUSR"登录失败.) [3] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] =>4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]无法打开登录请求的数据库tttb_db".登录失败.[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]无法打开登录请求的数据库tttb_db".登录失败.) )

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. ) )

我们的 SQL 服务器使用 Windows 身份验证,我们的服务器名称是 AEGIS-PC\SQLEXPRESS,用户名和密码块是灰色的.我想不出我们的登录失败的原因.我们做错了什么?

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.) 你的模块没有加载,因为它的 VC9 而不是 VC11.检查您的系统使用的编译器版本并安装正确的驱动程序.

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.) 检查您的 PHP 版本并为您的 PHP 版本使用正确的驱动程序,您可以在 phpinfo() 中检查.

2.) Check your PHP Version and use the correct driver for your PHP-Version your can check that in your phpinfo().

3.) 不要忘记安装 MSSQL Native Client 否则你无法连接到你的数据库,这是我每次遇到的问题.

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.

您的代码看起来不错,如果您收到未找到 sqlsrv_connect 的错误消息,则表明模块未加载.

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