2016-09-29 69 views
1

我有一個Windows Azure服務器,我想從PHP應用程序連接到MSSQL服務器。但是,當我嘗試連接到它會彈出一個錯誤信息說數據庫...我如何連接到MSSQL數據庫從PHP

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 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'.) [1] => Array ([0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'.)) 

我用這個代碼連接到數據庫..

<?php 
    $serverName = "AZR-SRV-MAP01"; //serverName\instanceName 

    // Since UID and PWD are not specified in the $connectionInfo array, 
    // The connection will be attempted using Windows Authentication. 
    $connectionInfo = array("Database"=>"EmpSys"); 
    $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)); 
    } 
?> 

我訪問使用數據庫Windows身份驗證,因此我沒有指定參數UIDPWD

請幫助我連接到SQL服務器。

+0

嘗試將''Trusted connection「=>」true「'添加到'$ connectionInfo'數組 – gofr1

+0

'$ connectionInfo = array(」Database「=>」EmpSys「,」Trusted connection「=>」true「); ' @ gofr1你的意思是這個對不對?在添加它之後,錯誤得到減少並且出現以下錯誤... 'Array([0] => Array([0] => IMSSP [SQLSTATE] => IMSSP [1] => -1 [code] => -1 [2] =>無效選項將可信連接傳遞給sqlsrv_connect。[message] =>無效選項將可信連接傳遞給sqlsrv_connect。))' –

+0

對不起,忘記了sqlsrv_connect默認使用可信連接:(什麼是你運行Apache,IIS? – gofr1

回答

0

工作代碼從PHP連接MSSQL數據庫(感謝gofr1)..

<?php 
    $serverName = "server_name"; //serverName\instanceName 

    // Since UID and PWD are not specified in the $connectionInfo array, 
    // The connection will be attempted using Windows Authentication. 
    $connectionInfo = array("Database"=>"db_name"); 
    $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)); 
    } 
?> 

編輯

你需要打開Windows身份驗證,並在IIS設置中設置匿名身份驗證關閉。通過您的本地/域帳戶授權給SQL Server。