2016-04-03 81 views
0

我使用ff。代码,但它有一个错误如何连接ms sql到php

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

+0

似乎是登录数据库的问题,你能确保你提供正确的凭据吗? – user1314742

+0

我已经检查过很多次了。细节是正确的 – Melo

回答

0

我个人使用:

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

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

0

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

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

+0

我已经有uname和pass的SQL服务器认证。 – Melo