dongyue3795 2015-03-31 07:13
浏览 158
已采纳

PHP连接到MS SQL Server 2012

I have a problem which I can't seem to fix. My task is simple : I want to connect to the machine in our network which is an used IBM Server and has SQL running on it.

My PHP connection script is as following :

// 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));
}

I am using the Microsoft SQL Server Management Tool and I have set up a Database with the name "hgadb" for test purpose as well as a Login for that database with the username hgadbuser and the matching password.

Everything seems to be set up, but I keep getting the same Error over and over again:

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

I know it's German - translated it says :

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

What am I missing here?

update

When I am trying to login with SSMS over SQL Server Authentification I get the following error message

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

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()

When I login with windows authentification and change the database the user is trying to connect to to the "master" database in the folder system database (as seen on the picture below), then i am able to connect to that one with SSMS and my php connection doesn't show any errors.

Why can't I connect to one of my test databases?

enter image description here

  • 写回答

1条回答 默认 最新

  • dps43378 2015-04-02 09:36
    关注

    Problem solved.

    I had to set Permissions for the Database in SSMS for Guest and Public connections. Now it works properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动