PHP trying to login to MSSQL (windows authenticati

2019-04-11 02:39发布

问题:

I am trying to establish a connection to my company's MSSQL server with windows authentication , but it fails as it is trying to use my computer name as login instead of my login id. When logging in with the MS SQL Server Management the windows authentication works fine, but not with this PHP code:

<?php
// Server in the this format: <computer>\<instance name> or 
// <server>,<port> when using a non default port number
$serverName = "xxx";
$connectionInfo = array( "Database"=>"xxx");

/* Connect using Windows Authentication. */
$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));
}

// 
?>

The printout I get is the following:

[Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'xxx\T2002197$'. ) )

T2002197 is my computer name, not my login id, so of course it fails. How can I solve this? I am using WAMP. Edited out some info, replaced with 'xxx'

回答1:

Aah problem solved! I changed the settings on my WAMP service (open service.msc in Windows) and made sure the service logged on the correct account. It works now.



回答2:

The manual says that per default the connection is using Windows Authentication and not the SQL Server authentication.

To bypass this you need to provide the uid and pwd options in your $connectionInfo data.

Se the manuals;

http://php.net/sqlsrv_connect

http://msdn.microsoft.com/en-us/library/ff628167.aspx