I have read from different forums, PHP website and Microsoft website how to connect PHP to MS SQL Server but the provided solutions didn't worked for me. If you could help me in this would be greatly appreciated.

Recommended Answers

All 8 Replies

Where is your code, and what error you get while running your script?

Member Avatar for Sanchit_Sahu

What is prob you are having..?? Atleast describe the prob so that we can help better

The first issue on connecting to a MSSQL database probably pertains to whether you are using and express version or not. The other problem will relate to which PHP drivers you have decided to use, there is a native Microsoft driver and the default PHP driver. I have discovered that it isquite a mission to get the PHP configuration correct.

Below a code snippet for native drivers.

$connectionInfo["UID"] = $username;
$connectionInfo["PWD"] = $password;
$connectionInfo["Database"] = "TESTDB";

$this->dbh = @sqlsrv_connect(  "127.0.0.1\SQLEXPRESS", $connectionInfo );

And you can download the drivers here

Click Here

I use MS SQL Server 2005 Enterprise edition and PHP 5.4.

Where is your code, and what error you get while running your script?

In addition to this (you still haven't answered), what exactly is your setup?

I run a WAMP in my PC(host-Win 7) and run the SQL Server in a virtual machine(Win Server 2003).

Did you set SQL Server to accept remote connections? Since the VM is a different machine PHP can't connect to localhost.

Thanks for replies guys, I was able to figure out the problem. I was using SQL Authentication in my PHP code but db server is using Windows Authentication. To correct the problem, I change server authentication then run the code again and this time it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.