我正在尝试将php连接到mssql数据库。我可以使用Windows身份验证或使用我创建的用户登录mssql。以下是php测试脚本:
<?php
$serverName = "SUPERMAN";
$connectionInfo = array('Database'=>'xikwambene');
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn) {
echo "Connection establish.<br />";
}else {
echo "Connection could not be established.<br />";
die(print_r(sqlsrv_errors(), true));
}
?>
但是我收到以下错误:
无法建立连接。 数组([0] =&gt;数组([0] =&gt; 28000 [SQLSTATE] =&gt; 28000 [1] =&gt; 18456 [code] =&gt; 18456 [2] =&gt; [Microsoft] [SQL Server Native Client 11.0] [SQL Server]登录失败,因为用户&#39; NT AUTHORITY \ SYSTEM&#39;。[message] =&gt; [Microsoft] [SQL Server Native Client 11.0] [SQL Server]用户登录失败&# 39; NT AUTHORITY \ SYSTEM&#39;。)[1] =&gt;数组([0] =&gt; 42000 [SQLSTATE] =&gt; 42000 [1] =&gt; 4060 [code] =&gt; 4060 [2] =&gt; [Microsoft] [SQL Server Native Client 11.0] [SQL Server]无法打开数据库&#34; xikwambene&#34;登录请求。登录失败。[message] =&gt; [Microsoft] [SQL Server Native客户端11.0] [SQL Server]无法打开数据库&#34; xikwambene&#34;登录请求。登录失败。)[2] =&gt;数组([0] =&gt; 28000 [SQLSTATE] =&gt; 28000 [1] =&gt; 18456 [code] =&gt; 18456 [2] =&gt; [Microsoft] [SQL Server Native Client 11.0] [SQL Server]用户登录失败了NT AUTHORITY \ SYSTEM&#39;。 [message] =&gt; [Microsoft] [SQL Server Native Client 11 .0] [SQL Server]登录失败,用户“NT AUTHORITY \ SYSTEM&#39;”。 )[3] =&gt;数组([0] =&gt; 42000 [SQLSTATE] =&gt; 42000 [1] =&gt; 4060 [code] =&gt; 4060 [2] =&gt; [Microsoft] [SQL Server Native Client 11.0] [SQL Server]无法打开数据库&#34; xikwambene&#34;登录请求。登录失败。[message] =&gt; [Microsoft] [SQL Server Native Client 11.0] [SQL Server]无法打开数据库&#34; xikwambene&#34 ;登录请求。登录失败。))
此错误表示由于登录详细信息失败而无法建立连接。如果我需要在我的代码中添加登录详细信息,我将如何处理它?请协助
答案 0 :(得分:2)
从手册:
打开与Microsoft SQL Server数据库的连接。默认情况下,使用Windows身份验证尝试连接。要使用SQL Server身份验证进行连接,请包含&#34; UID&#34;和&#34; PWD&#34;在连接选项数组中。
所以在你的connectioninfo中添加:
$connectionInfo = array('Database'=>'xikwambene', "UID"=>"userName", "PWD"=>"password");