jump to navigation

Connecting to SQL Server 2005 Express using PHP March 9, 2008

Posted by razasayed in programming.
Tags:
trackback

Recently i was trying to connect to a SQL Server 2005 Express database using PHP on a XP machine and i got the following error :

mssql_connect() [function.mssql-connect]: Unable to connect to server…

The 8 steps that i followed to resolve this error are as follows :

1.) Downloaded ntwdblib.dll (version : 2000.80.194.0) from Webzila.com

2.) Copied this dll file to apache\bin

3.) Restarted the apache service by going to Control Panel->Administrative Tools->Services.

4.) Went to “SQL Server Configuration Manager”.

5.) Under “SQL Server Network Configuration” clicked on “Protocols for SQLExpress”.

6.) On the right hand side,right clicked on “Named Pipes” and clicked on Enable

7.) Also, right clicked on TCP/IP and clicked on enable

8.) Restarted the SQL Server Express service. You can do that from the control panel or by issuing the following two commands at the command line :

net stop mssql$sqlexpress

net start mssql$sqlexpress

That was it ! . I was now able to connect to SQL Server 2005 Express :) .

In case it still doesnt work for you try the following :

1) Copy php_mssql.dll to apache\ext.

2) Make sure that in your php.ini file , extension=php_mssql.dll is uncommented.

3) Make sure you are using the right port number in your code. You can find out what port SQL Server is listening on by doing the following :

3.1) Go to “SQl Server Configuration Manager”.

3.2) Under “SQL Server Network Configuration” click on “Protocols for SQLExpress”.

3.3) On the right hand side window, right click on TCP/IP , click on Properties.

3.4) In the new window click on the “IP Addresses” tab

3.5) The value of “TCP Dynamic ports” is the port number that SQL Server is listening on.

I spent a hell lot of time figuring all this out, and i hope that this post saves you all that trouble.

Happy Hacking ! :)

Comments»

1. Nishant Patel - May 6, 2008

hey raza,

How are you. I just came across your blog about sql server connection. I am having problems connecting linux to sql server :)

N

2. razasayed - May 6, 2008

Hello sir, im doing fine :) …i think the FreeTDS driver is required for connecting to SQL Server from Linux..here is something i came across which might help : http://www.linuxjournal.com/node/6636/print

3. William Chiam - June 1, 2008

I was having the same problems. I was using IIS with PHP 5 and SQL Express 2005 though. Anyway, I followed your steps and got it working. In my case, I copied the dll file to my PHP folder, overwriting the current copy and restarted the IIS. It worked.. like a charm! Thanks!

4. Troy - August 8, 2008

Thanks Raza, as with William I experienced the same problem after upgrading to SQL Express 2005 with PHP on IIS. Worked a treat!!

5. Troy - August 8, 2008

A point of caution, if you search mozilla for ntwdblib.dll, make sure you download the first of the two files displayed, unless you like your date output in what appear to be French.

6. razasayed - August 8, 2008

Thanks for the tip troy :)

7. Uwe Seeliger - August 13, 2008

Thanks for your explanation and tip - helped me a lot.

8. razasayed - August 13, 2008

You are welcome Uwe….im glad you found the post useful :)

9. Angelus - August 18, 2008

A most helpful blog…..thank you kindly for the insight.