2015-03-31 214 views
1

我有一個問題,我似乎無法解決。 我的任務很簡單:我想連接到我們的網絡中的機器,這是一臺使用過的IBM服務器,並且運行了SQL。PHP連接到MS SQL Server 2012

我的PHP連接腳本如下:

// Melde alle PHP Fehler 
error_reporting(-1); 
ini_set('display_errors', 1); 
// Server in diesem Format: <computer>\<instance name> oder 
// <server>,<port>, falls nicht der Standardport verwendet wird 

error_reporting(E_ALL); 
$serverName = "SERVERNAME\MSSQLSRV"; 

$connectionInfo = array('Database'=>'hgadb', "UID"=>"USERNAME", "PWD"=>"PASSWORD"); 
$conn = sqlsrv_connect($serverName, $connectionInfo); 


if($conn) { 
    "Connection established.<br />"; 
}else { 
    "Connection could not be established.<br />"; 
    die(print_r(sqlsrv_errors(), true)); 
} 

我使用的是Microsoft SQL Server管理工具,我已經建立了一個名爲「hgadb」用於測試目的的數據庫,以及對登錄該數據庫使用用戶名hgadbuser和匹配的密碼。

一切似乎成立,但我一直一遍又一遍地得到同樣的錯誤:

Array ([0] => Array ([0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Fehler bei der Anmeldung f�r den Benutzer 'USERNAME'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Fehler bei der Anmeldung f�r den Benutzer 'USERNAME'.) [1] => Array ([0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Die von der Anmeldung angeforderte hgadb-Datenbank kann nicht ge�ffnet werden. Fehler bei der Anmeldung. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Die von der Anmeldung angeforderte hgadb-Datenbank kann nicht ge�ffnet werden. Fehler bei der Anmeldung.) [2] => Array ([0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Fehler bei der Anmeldung f�r den Benutzer 'USERNAME'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Fehler bei der Anmeldung f�r den Benutzer 'USERNAME'.) [3] => Array ([0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Die von der Anmeldung angeforderte hgadb-Datenbank kann nicht ge�ffnet werden. Fehler bei der Anmeldung. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Die von der Anmeldung angeforderte hgadb-Datenbank kann nicht ge�ffnet werden. Fehler bei der Anmeldung.)) 

我知道這是德文 - 翻譯它說:

[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]The from the login requested database: DATABASENAME-Database cannot be opened. Error with login.)) 

我在想什麼這裏?

更新

當我試圖用SSMS在SQL服務器驗證登錄我收到以下錯誤消息

=================================== 

Cannot connect to SQL\MSSQLSRV. 

=================================== 

Die Standarddatenbank des Benutzers kann nicht geöffnet werden. Fehler bei der Anmeldung. 
Fehler bei der Anmeldung für den Benutzer 'USERNAME'. (.Net SqlClient Data Provider) 

------------------------------ 
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&EvtSrc=MSSQLServer&EvtID=4064&LinkId=20476 

------------------------------ 
Server Name: SERVERNAME\MSSQLSRV 
Error Number: 4064 
Severity: 11 
State: 1 
Line Number: 65536 


------------------------------ 
Program Location: 

    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 
    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 
    at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 
    at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) 
    at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) 
    at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) 
    at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) 
    at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) 
    at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) 
    at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) 
    at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) 
    at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) 
    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) 
    at System.Data.SqlClient.SqlConnection.Open() 
    at Microsoft.SqlServer.Management.SqlStudio.Explorer.ObjectExplorerService.ValidateConnection(UIConnectionInfo ci, IServerType server) 
    at Microsoft.SqlServer.Management.UI.ConnectionDlg.Connector.ConnectionThreadUser() 

當我登錄使用Windows認證,改變用戶試圖數據庫連接到文件夾系統數據庫中的「主」數據庫(如下圖所示),然後我可以使用SSMS連接到該主數據庫,並且我的php連接不顯示任何錯誤。

爲什麼我不能連接到我的一個測試數據庫?

enter image description here

+0

服務器名稱丟失 – 2015-03-31 07:22:13

+0

您的用戶是否有足夠的權限訪問數據庫?當您從SSMS與該用戶連接時,您是否可以打開並查詢數據庫?請檢查SQL Server日誌並與我們分享與失敗登錄相關的錯誤條目的狀態,這有助於瞭解錯誤的來源。 – Pred 2015-03-31 07:22:56

+0

服務器名稱正在sqlsrv_connect函數中聲明和調用:) 明天我會分享我的日誌 - 複製我必須通過SQL Server身份驗證連接到SSMS的錯誤嗎? 我正在使用SSMS連接Windows身份驗證。 – 2015-03-31 17:38:59

回答

0

問題解決了。

我必須爲訪客和公共連接的SSMS中的數據庫設置權限。現在它工作正常。

+0

您在SSMS中設置了這些權限?在實例上或特定的數據庫和哪個文件夾?安全等。?謝謝。 – 2016-09-27 21:24:33

+1

右鍵點擊你的數據庫 - >屬性 - >權限 – 2016-09-29 07:03:07