2016-04-03 106 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