jump to navigation

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

Posted by razasayed in programming.

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 ! 🙂



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 🙂


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.

10. Bill - October 17, 2008

I was just trying to do the same thing and just simply used an ODBC connection.

11. Alex - January 26, 2009

Many thanks, saved me a lot of time – I remembered having to do this years ago for SQL Server 2000 but it all looks a bit different in 2005!

12. Jason - April 25, 2009

Wonderful! This is the only resource I could find on the Internet that helped. Thank you! I thought I’d also add some related code to help get the ball rolling for other people:

$conn = mssql_connect(“localhost\SQLEXPRESS,3292”, “myusr”, “mypwd”) or die(“Couldn’t connect to SQL Server”);

mssql_select_db(‘mydb’, $conn) or die(“Couldn’t connect to Server database.”);

$stmt = mssql_init(“myproc”, $conn);
mssql_bind($stmt, “@param1”, $param1, SQLVARCHAR, FALSE);
$result = mssql_execute($stmt);

while($row = mssql_fetch_array($result)) {
echo “” . $row[“id”] . $row[“name”]. “”;


13. DiamondEagle - April 28, 2009

Thanks. But isn’t ntwdblib.dll depricated?

14. Patrick - June 4, 2009

did you use the Microsoft SQL Server Driver for PHP??

15. Anonymous - June 14, 2009

gandi baaat

16. Connect auf SQLEXPRESS via PEAR::MDB2 - php.de - July 16, 2009

[…] eher Datenbanken ist, weiß ich gar nicht. Nach einiger Suche bin ich auf diese Seite gestoßen: Connecting to SQL Server 2005 Express using PHP � Raza’s Weblog Das hat schon mal dazu geführt, dass der Connect mit pdo funktioniert. Allerdings bekomme ich es […]

17. Hitesh Wadhwa - September 23, 2009

Hello Guys,

Please help, I get the following message
Warning: mssql_query(): supplied argument is not a valid MS SQL-Link resource in C:\wamp\www\test\sql2005\index.php on line 15
MSSQL error: Changed database context to ‘StarWatch’.

My code is as follows.

18. Hitesh Wadhwa - September 23, 2009

Hello Guys, please help ,,.
I get the following message
Warning: mssql_query(): supplied argument is not a valid MS SQL-Link resource in C:\wamp\www\test\sql2005\index.php on line 15
MSSQL error: Changed database context to ‘StarWatch’.

My code is as follows.

$server = ‘.\SQLEXPRESS’;
$link = mssql_connect($server, ‘sa’, ‘password’);
die(‘Something went wrong while connecting to MSSQL’);
$dblink=mssql_select_db(‘StarWatch’, $link);
die(‘Something went wrong while connecting to DB’);
$sql=”select FirstName from IDHolder”;
$qry=mssql_query($sql,$dblink); // or die(“Error : Table Failed”);
die(‘MSSQL error: ‘ . mssql_get_last_message());

//echo $qry;
echo mssql_num_rows($qry);
while ($row=mssql_fetch_array($qry)){
echo $row[“FirstName”];

19. Alejandro Cordoba - March 8, 2010


Estuve buscando esta información en muchos sitios y no me funcionaba, pero con este manual lo tengo funcionando perfectamente. Aquí pongo mi código para los que deseen complementar.

// Server in the this format: \ or
// , when using a non default port number
$server = “localhost,1879”;

// Connect to MSSQL
$link = mssql_connect($server, “user”, “password”);

if (!$link) {
die(“algo falla en con la conexion”);

die(“algo anda mal al conectar a la base de datos”);
$sql=”select * from cars”;
$qry=mssql_query($sql,$link); // or die(“Error : Table Failed”);
die(“MSSQL error: “.mssql_get_last_message());

//echo $qry;
echo mssql_num_rows($qry);
while ($row=mssql_fetch_array($qry)){
echo $row[“name”];

La base de datos que utilicé:

USE examples;
name varchar(40),
year varchar(50),

INSERT INTO cars VALUES(1,’Mercedes’,’2000′);
INSERT INTO cars VALUES(2,’BMW’,’2004′);
INSERT INTO cars VALUES(3,’Audi’,’2001′);

20. Prakash Nanjan - July 1, 2010

Hey Raza , Thanks a lot , this started working out , the only thing i missed during the connectivity was to enable the pipes and tcp/ip , coz i wasn’t that updated about the services of the sql server …. Any how thanks again 🙂

21. user - September 23, 2010

Thanks for this nice help!

and i have work out with these steps:
– put/replace ntwdblib.dll to the apache/bin folder
– the php code as follows:
– don’t forget that the port is required!, and it is described as TCP Dynamic Port in IP All section for sure.

22. Gaurang - October 7, 2010

how can i connect to sql server using php on remote pc.
i want to use that sql server connection in linux pc and my sql server is on window pc and both are in network

thanks in advance

23. luffy - October 26, 2010

Hi, Raza i have an apache web server and php5 in linux PC and i want to connect to mssql server in another windows PC. Have you ever try that? If you ever try you will save my life. Thanks.

24. df - November 4, 2010


25. Peter - December 11, 2010

thanks… it really help a lot…

26. Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: « ออกแบบเว็บไซต์ เขียนโปรแกรม ทำเว็บไซต์ - December 19, 2010
27. cris - January 2, 2011

a lot of Thanks for save me a lot of time!!

28. Divy - January 6, 2011

Hi Thnaks for this great blog…

but its not working for me, i have tried all the things given above and also from different references but i am getting the error says “PHP Fatal error: Call to undefined function mssql_connect()”. Please help….

29. Anonymous - March 22, 2011

Hello sir. I am going to design an application using php 5 and sql server 2008 express edition. sir i am facing a problem the functions mssql_connect() are not found and server is also not found messages are displayed. kindly give some solution.

30. Leonardo Ramirez - May 18, 2011

Hi, Actually, I have a SQL Server Express 2005. And It’s working with some aplication remotly. Now I install a web server with OS Centos (linux). But I can’t connect to DB.

“Couldn’t connect to SQL Server on\SQLEXPRESS”

I used PHP….

$myServer = “\SQLEXPRESS”;
$myUser = “user”;
$myPass = “pwd”;
$myDB = “db”;

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die(“Couldn’t connect to SQL Server on $myServer”);

Somebody know how can I fix it?

31. Issam Ouchen - July 9, 2011

Thank You Soo Much !!

32. Stephany - January 21, 2013

Thanks for the interesting things you have revealed in your article.
One thing I want to comment on is that FSBO connections are built
eventually. By introducing yourself to owners the first saturday and sunday their FSBO is
announced, before the masses start calling on Friday, you produce a good link.
By mailing them resources, educational products, free reviews, and forms, you become a great ally.
By subtracting a personal desire for them as well as
their scenario, you develop a solid network that, on many occasions, pays off in the event the
owners opt with an agent they know as well as
trust preferably you actually.

33. sk - February 16, 2013

more source code then log on to http://www.mca.somee.com

34. helpful resources - March 17, 2013

Heya i am for the first time here. I found this
board and I find It really useful & it helped me out a lot.

I hope to give something back and aid others like you helped

35. トリーバーチ - May 16, 2013

I blog quite often and I truly thank you for your content.
This article has really peaked my interest. I’m going to book mark your website and keep checking for new details about once per week. I opted in for your Feed too.

36. Quinn - May 19, 2013

Tremendous issues here. I am very glad to peer your post.

Thanks a lot and I’m taking a look forward to contact you. Will you kindly drop me a mail?

37. Genevieve - May 30, 2013

The exterior is actually an excellent black colored, creating it difficult in order to see the coffee which you have spilled in it over the years, and additionally it
doesn’t really come aside, therefore besides the filter basket and the pot, your don’t
need to worry about cleaning it. I would try to be
really surprised should you decide could uncover this particular West Bend Quick – Drip 10-cup coffee maker for sale
anyplace, however you may well be effective discover
the used one online, potentially on craigslist, e –
Bay, or Bid – Cactus.

38. コーチ バッグ - June 25, 2013

Hey there! I realize this is kind of off-topic however I
needed to ask. Does running a well-established blog such as yours take a massive amount work?
I’m completely new to writing a blog however I do write in my diary on a daily basis. I’d like
to start a blog so I can share my experience and thoughts online.
Please let me know if you have any ideas or tips for brand new aspiring blog owners.
Appreciate it!

39. Optimal Stack Reviews - September 26, 2013

Way cool! Some extremely valid points! I appreciate
you penning this article and the rest of the site is very good.

40. Redondo BeachRealtor - October 1, 2014

Excellent post. I used to be checking constantly this
blog and I am impressed! Extremely helpful info
specially the ultimate section 🙂 I deal with such information much.
I was seeking this certain info for a long time.

Thanks and good luck.

41. M Faried Dzh - October 17, 2016

THanks so much bro, and hope for you always in a good time.

42. comparatif tirebouchon electrique - February 14, 2017

There is definately a great deal to know about this issue.
I love all the points you’ve made.

43. moving wordpress from localhost to server - February 21, 2018

What i do not realize is in truth how you’re no longer really a lot more neatly-favored than you may be right now.

You’re so intelligent. You recognize thus considerably when it
comes to this matter, made me individually believe it from a lot of numerous angles.
Its like women and men aren’t involved unless it’s one thing
to do with Lady gaga! Your personal stuffs nice. Always handle it up!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: