【问题标题】:how to connect ms sql to php如何将 ms sql 连接到 php
【发布时间】:2016-07-22 22:36:30
【问题描述】:

我用的是ff。代码,但它有一个错误

<?php
$serverName = "MELODY-PC\SQLEXPRESS"; //serverName\instanceName
$connectionInfo = array( "Database"=>"customerdb", "UID"=>"admin", "PWD"=>"reden");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>
错误如下:

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

【问题讨论】:

  • 好像是登录数据库的问题,你能确定你提供了正确的凭据吗?
  • 我已经检查过很多次了。细节是对的

标签: php sql sql-server


【解决方案1】:

我个人使用:

if(mssql_connect('server_ip','user','password') === FALSE) die('Error Connecting to MySQL Server.');

但请注意,此功能已弃用。

【讨论】:

    【解决方案2】:

    您的用户似乎已配置为使用 Windows 身份验证,但为了使用 sqlsrv_connect() 和凭据(UID 和 PWD),用户必须具有 SQL Server 身份验证,而不是 Windows 身份验证。

    This answer 展示了如何创建 SQL Server 身份验证用户。

    【讨论】:

    • 我已经使用 uname 进行 SQL Server 身份验证并通过了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2014-02-16
    • 2017-01-29
    • 2018-06-09
    相关资源
    最近更新 更多