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.

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"]. “”;
}

mssql_close($conn);

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’);
if(!$link)
die(‘Something went wrong while connecting to MSSQL’);
$dblink=mssql_select_db(‘StarWatch’, $link);
if(!$dblink)
die(‘Something went wrong while connecting to DB’);
$sql=”select FirstName from IDHolder”;
$qry=mssql_query($sql,$dblink); // or die(“Error : Table Failed”);
if(!$qry)
die(‘MSSQL error: ‘ . mssql_get_last_message());

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