daveomcd
daveomcd

Reputation: 6555

PHP: How do I establish a connection to my SQL Server?

I usually use my SQL Server with SQL Server Management Studio. When logging in there I use Windows Authentication and do not specify a Username/Password. So the username/password I'm using at the moment is the same I use logging onto my computer. I'm attempting to establish a connection to the server through PHP; however, I get the an error. Anyone know what I am doing wrong?

Code

    $serverName = "MYPCNAME"; 

    $connectionInfo = array( "Database"=>"College", "UID"=>"MYUSERNAME", "PWD"=>"MYPASSWORD");

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

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 'MYUSERNAME'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'MYUSERNAME'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'MYUSERNAME'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'MYUSERNAME'. ) )

Upvotes: 1

Views: 8369

Answers (3)

Yohanes Gultom
Yohanes Gultom

Reputation: 3842

I think the problem is in your SQL Server configuration. You should have a user with SQL Server Authentication instead of Windows Authentication. Here is how https://support.gearhost.com/KB/a453/adding-users-to-mssql-using-sql-server-management-studio.aspx

And you may also want to check if your SQL Server TCP/IP configuration is accessible remotely : http://blogs.msdn.com/b/walzenbach/archive/2010/04/14/how-to-enable-remote-connections-in-sql-server-2008.aspx

Upvotes: 2

OperatorOverload
OperatorOverload

Reputation: 724

I think you're not going to be able to connect to SQL from PHP using Windows authentication, you'll likely need to enable SQL Authentication as well and connect with the SA or other SQL-level account.

Upvotes: 0

Daryl Gill
Daryl Gill

Reputation: 5524

You should consider switching to SQL Server Authentication which enables you to use a range of accounts.

It seems the login you are using is in-correct; I have used SQL Server in the past, and during registration you should (correct me if i'm wrong) be asked for a password for "sa"

SQLSTATE = 28000 - Review This

and tell see if this provides you with a solution?

Also, Review The following link IF you are using Windows Authentication:Connect to MSSQL Server Using Windows Authentication

Upvotes: 0

Related Questions