sqlsrv_connect登录失败的用户

我正在尝试使用PHP连接到我的SQL Server数据库 . 我正在运行WampServer和SQLExpress及其Management Studio . 出于某种原因,调用sqlsrv_connect失败 . 已打开与SQL Server的连接,但用户登录失败 . Windows身份验证和SQL Server身份验证都会发生这种情况 . 我已经在数据库属性的“安全”选项卡下切换到“SQL Server和Windows身份验证模式” . 我还检查了“连接”选项卡下是否允许远程连接 . 我可以使用带有Management Studio的SQL Server身份验证登录,而不是使用PHP代码 . 对我可能做错的任何帮助表示赞赏 .

<?php
$serverName = "STTM-PC\SQLEXPRESS";
$connectionInfo = array("Database"=>"myDatabase","UID"=>"myUsername","PWD"=>"myPassword");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn) {
    echo "Connection Established
"; } else { echo "Connection Failed
"; die( print_r( sqlsrv_errors(), true)); } ?>

打印出来

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 'myUsername'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'myUsername'. ) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "myDatabase" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "myDatabase" 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 'myUsername'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'myUsername'. ) [3] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "myDatabase" requested by the login. The login failed. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot open database "myDatabase" requested by the login. The login failed. ) )

回答(1)

2 years ago

我弄清楚出了什么问题 . 我从来没有用户到数据库,因此没有 Build 连接 . 为了解决这个问题,我使用Management Studio登录了数据库 . 我进入“安全”选项卡,然后是“登录”选项卡 . 我右键单击了用户名和所选属性 . 在“用户映射”下,我选择“myDatabase”将其映射到此帐户 . 现在, Build 连接 .