kya
kya

Reputation: 1828

connecting php to mssql database

I am trying to connect php to mssql database. I can login to the mssql using windows authentication or using a user I created. The following is a php test script:

<?php

$serverName = "SUPERMAN";

$connectionInfo = array('Database'=>'xikwambene');
$conn = sqlsrv_connect($serverName, $connectionInfo);

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


?>

But I get the following error:

Connection could not be established. Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'NT AUTHORITY\SYSTEM'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'NT AUTHORITY\SYSTEM'. ) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "xikwambene" requested by the login. The login failed. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "xikwambene" requested by the login. The login failed. ) [2] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'NT AUTHORITY\SYSTEM'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'NT AUTHORITY\SYSTEM'. ) [3] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "xikwambene" requested by the login. The login failed. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "xikwambene" requested by the login. The login failed. ) )

This error shows that connection could not be established due to login details failure. if I need to add login details to my code, how will I go about it ? Please assist

Upvotes: 1

Views: 1820

Answers (1)

Daan
Daan

Reputation: 12236

From the manual:

Opens a connection to a Microsoft SQL Server database. By default, the connection is attempted using Windows Authentication. To connect using SQL Server Authentication, include "UID" and "PWD" in the connection options array.

So in your connectioninfo add this:

$connectionInfo = array('Database'=>'xikwambene', "UID"=>"userName", "PWD"=>"password");

Upvotes: 2

Related Questions