0

I use the ff. codes but it has an error

<?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));
}
?>
the error were as follows:

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. ) )
MrCode
  • 63,975
  • 10
  • 90
  • 112
Melo
  • 23
  • 3

2 Answers2

0

I'm personally using:

if(mssql_connect('server_ip','user','password') === FALSE) die('Error Connecting to MySQL Server.');

But please note that this function is deprecated.

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
0

It appears your user is configured to use Windows Authentication but in order to use sqlsrv_connect() with credentials (UID and PWD), the user must have SQL Server Authentication, not Windows Authentication.

This answer shows how to create a SQL Server Authentication user.

MrCode
  • 63,975
  • 10
  • 90
  • 112