AKA Marketing.com Logo            VISIT THE BLOG            

Blogged thoughts, is our web blog. Expect views, opinion, rants and tirades about everything and anything 

« Home / Forums »        

 


Subscribe to our SEO / IT related blog by entering your email address below

Blogged thoughts

| by the www.akamarketing.com team



PHP SQL Server connection problems - mssql_connect() [function.mssql-connect]: Unable to connect to server

As pear (pun intended) usual if I have what I believe is a useful solution to something I’ve spent an eternity trying to fix in my code I will try put it up on the old blog for the benefit of others. The problem I was having for a large part of yesterday and earlier on this morning (Tuesday the 12th) related to the use of a PEAR based Database Abstraction Layer (DAL) module called DB. For those that don’t know a DAL allows a developer to call generic database access code, which then in turn calls DB provider specific code. The advantage of this is that the developer can change the underlying database system by changing a single line of code in a connection string as opposed to all DB access logic in his or her code.

The Database Abstraction Layer I was using: DB is actually depreciated but I was using it for legacy purposes as most of our existing PHP apps use it. The thing to remember with DALs is that ‘under the hood’ they will eventually call DB provider specific code so problems with DB, any other PEAR DAL or indeed any DAL in general may not be caused by the DAL itself but in fact caused by the underlying DB provider specific code.

My problem in this case was that I just seemed to keep getting connection errors when I called the ‘connect’ function in DB. The code I used was similar to:

require_once(’DB.php’); //makes the DB extension available to my code

//connection string for SQL Server database
$db = DB::connect(”mssql://username:PaSsWoRd@dbhost/dbname”);

if(DB::iserror($db))
{
       die($db->getMessage());
}

and the error I got was DB Error: connect failed. As you can see my underlying database is SQL Server as indicated by ‘mssql’ in the above code.
I’m not too sure about the more recent releases of DB (I was using one from 2003) but it seemed DB was not very useful from an error debugging point of view as it didn’t ‘bubble up’ all error messages and only spit out something very generic and very useless.

The thought struck me that perhaps the underlying PHP MS SQL function library was not enabled in the PHP configuration file. This turned out to be the case. Enabling it was as simple as adding a line or two into the PHP.ini file:

[PHP_MSSQL]
extension=php_mssql.dll

If you had of called

$db = mssql_connect(”dbhost”,”username”,”PaSsWoRd”);

directly in your code, which the above PEAR DB code eventually did you would have got a much more helpful error like the following:

PHP Fatal error: Call to undefined function mssql_connect() in…

which you would (hopefully) immediately diagnose as being related to the availability of the PHP mssql library itself and not DAL related.

After enabling the extension, I was still getting the DB Error: connect failed generic error from PEAR DB so I decided to work directly with the mssql_connect function to see if again it was a SQL Server issue. When I called mssql_connect I still couldn’t get a connection and got the error below so it was obviously not PEAR DB playing up.

PHP Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: servername

It turns out that my connection string was 100% correct however the version of the ntwdblib.dll file on my PHP box was not compatibile with certain recent versions of SQL Server. According to the PHP website ntwdblib.dll is required for the PHP MSSQL extension to work:

The extension requires the MS SQL Client Tools to be installed on the system where PHP is installed. The Client Tools can be installed from the MS SQL Server CD or by copying ntwdblib.dll from \winnt\system32 on the server to \winnt\system32 on the PHP box. Copying ntwdblib.dll will only provide access through named pipes. Configuration of the client will require installation of all the tools.

It seems that for whatever reason the very latest installs of PHP include a version of ntwdblib.dll that will not work with SQL Server 2003, SQL Server 2005 and as far as I’m aware SQL Server Express. The version of ntwdblib installed is likely to be 2000.2.8.0 when what you need to have to talk to recent versions of SQL Server is 2000.80.194.0. This file can actually be present in locations other than winnt\system32 depending on your platform and installation setup so I suggest you do a search for it, check the version and if it doesn’t end it 80.194.0 download the latest version from the UserScape.com site and use it to overwrite the existing version. In my case I installed PHP as a CGI on Windows Server 2003 so the file was present directly in the PHP folder. When I updated it and tried my code again everything worked fine, including the original PHP DAL DB:connect call.

In the end it turned out my problems were nothing to do with the PEAR DB module but were related to PHPs SQL Server functions. If your still having problems connecting to SQL Server from PHP I suggest you visit the relevant PHP page located at http://ie2.php.net/function.mssql-connect which contains a lot of user contributed information about ntwdblib.dll and other issues which may be causing your problems and associated pain.

51 Comments on “PHP SQL Server connection problems - mssql_connect() [function.mssql-connect]: Unable to connect to server”
1| Larry said,

Thanks for posting this. Solved my problem. It was driving me crazy

2| Faisal S said,

Thanks! That worked

3| Igor said,

Thank you for your post. I’ve got connected at last)

4| Arsen Abdrakhmanov said,

Great!
It required several hours of web surfing before I’ve found your page with answer to my problem related to the version of ntwdblib in php package.

Thank you! Great job!

5| Wachingol said,

thank you, thank you, thank you
you are my hero

Saludos desde México!!!

6| Abhinav Shine said,

Great ! Solved the hell problem faced by me ! Thanks for the detailed Note .

7| Dave_Jones said,

Fantastic work!
Saved me a lot of soul searching.

Thanks and big ups.

8| Chris Bergeron said,

Your solution worked! One thing I noticed in my xampp installation, was that I had to replace the ntwdblib.dll in the apache2/bin directory as well as in the php and windows/system32 directories.

I restarted apache and it connected!

Thanks a million! You saved me from a lot of frustration!

9| Arsyan said,

Thanks man ! I googled and found your page the first. Really helped me alot! Cant believe a simple dll change fixed everything.

10| Hamed Afshar said,

I really appreciate this article which solved my problem perfectly.
It was a nightmare, Thanks :)

11| Mikael said,

thx, solved my prolem :D

12| Edgar and David said,

Thanks for your help. The problem has been resolved.

13| Vipin said,

Thanks to the person who wrote this.
We had no clue of this.
Also, the problem was solved and it saved a lot of time.
Thanks again.

14| Daniele Salatti said,

Unfortunately I have a slightly different problem… Anyway, thanks forsharing…

15| bonfa53 said,

Thanks, now it works!!! (xampp + mssql 2005)

16| Ricardo Vargas said,

Thanks a lot!!! Work Amazing.
Apache 2.2.11 + PHP 5.2.8 + MySQL 5.1 + MsSQL 2005

ntwdblib.dll was the problem. Microsoft need to open a website for apache users, and update the last news libraries and stuff.

Well, thanks again. :)

17| Raj said,

Hi,
I have installed SQL Server 2008+ IIS6.0+PHP 5.2.8
I have uncommented the mssql extension in php.ini
Placed the ntwdblib.dll in C:\PHP & C:\WINDOWS\system32 directory

I am working on Windows Server 2003 R2

I am still getting the error:

Checking PHP DB extensions Warning!: Your PHP installation don’t have the mssql extension mssql- without it is IMPOSSIBLE to use Testlink.Failed!

I am using SQL server authentication (not windows authentication)

Should I consider installing Native client? or re-install the SQL server 2008 again?

I am new to IIS & SQL server.

I checked phpinfo.php, I can see all the extensions except mssql.

Please helpppp !!!!!

Thanks,
Raj

18| bmedek said,

Hi Raj,

maybe it will help, maybe not, but it was the solution for me.
mssql_connect(”localhost”,”sa”,”pass”) was not working. After enabling the TCP\IP protocol in SQL Server Configuration Manager for my db instance (called MyDB) and changing the connect script to:
mssql_connect(”localhost\MyDB”,”sa”,”pass”) i was able to connect to MSSQL 2008.

Copying the ntwdblib.dll file to PHP\Ext directory was not the solution. The most recent version is already installed in the system32 directory.

19| bmedek said,

Just for clarification. I am using SQL Server 2008 Express + Apache 2.2 + PHP 5.2.9 on WinXP(64bit)

20| Alex Mattelaer said,

Very useful article, thank you - I found it all started working once I had installed the correct version of ntwdblib.dll *and* restarted the following services:
- SQL Server
- SQL Server Browser
- Apache.

21| Jeremy said,

Very helpful! Was lead to this article by support pro!

22| Totti said,

Thanks a lot, this has been driving me crazy till I found your article. :D

My Config: Vista, PHP 5.1, SQL 2k5, Apache 2.

23| fulldeto said,

Excelent job.

24| Amit said,

Working Apache 2.2.11 + PHP 5.2.9-1+ MsSQL Server2005. Have done all of the above, but to avail. Gives me error, “Call to undefined function mssql_connect()”.

I have already followed all of the above methods to get the corrct dll’s and stuff. Nothing seems to be working.

Help would be greatly appreciated

25| Miguel Angel said,

Thank you very much. Why haven’t they fixed that in new version of PHP?

26| khottabit84 said,

thanks you so much for your help! I’m a beginner and this problem taked me time consuming to find out the reason.

27| Mahmud said,

i have tried the solution still its not working. “Unable to connect to server: 192.168.80.104,1433 “… Can anyone suggest any solution.

28| pAo. said,

Amit, make sure if you configured your php.ini

29| Victor said,

Thanks for the solution. This was the last of several problems trying to get the MSSQL connection working out of PHP, and it’s taken me all morning. I need to use PHP for a particular web site, but as a professional developer I would normally avoid PHP like the plague, and use more contemporary technologies, such as ASP.NET. The mountain of time that can be wasted trying to work worth open source, and poorly supported, software is just not economical in the commercial (read: real) world.

30| Matt said,

This ‘bug’ was driving me crazy. I require to use Windows for certain parts of my website and I tried instlaling XAMPP on several of my servers and each failed to connect to our MSSQL database for no reason.

After hours of debugging (with no luck) and random Google searches, I came across your blog and boom -> fixed!

Thanks a bunch. Keeping the DLL on my local PC and a link to this incase I get these problems in the future!

Cheers!

31| Madhi said,

Thanks. i was breaking my head for two days to resolve the mssql connectivity problem. This blog really helped me to fix the Issues.Thanks once again.

32| john said,

hi there, im trying to connect to mssql server via http..

is it possible?

33| Michael Fall said,

Very very very useful article, thank you
Using:
- Windows XP Pro
- WAMP5
I copied the new dll to …wamp\Apache2\bin and …wamp\php *and* restarted the following services:
- Apache
- Eg. WAMP5

34| Jesus said,

Very nice article. It works for me too! Greetings from Puerto Rico!

35| Mahmud said,

Finally it worked. I had to copy the file in apache/bin/ too. Thnx a lot guys

36| Gth said,

Thank you very very much for the ntwdblib.dll version tip

37| Satheesh said,

Thanks a lot…
I cant connect the sql server 2005 with php in my system, I spent more than a week to fix this problem, Nice article its saved my life. Now my application is working fine…
Thank you once again for this post…

38| gunasekaran said,

If I call the mssql_connect function in php its show the error message like(function undefined). then how will include the mssql_connect function in PHP

39| sandip said,

Hi there,

thats work, very useful after struggling 2 hour. Thank chaps

40| Pradip Jadhav said,

Thanks a lot buddy…
I am trying to connect sql server 2005 since last week.
Your post really help me to fix out the problem
Thank you once again for this post

41| Anonomous said,

Thanx, solved my problem.

This is still a problem in PHP 5.2.9 (bundled in WAMP server)

42| Francis Edsel said,

Thanks a lot… I solve my issues

43| feras said,

thank you so much for your efforts
this solve my problem to connect mssql by php

44| Gary said,

Mate,

Wish I read this article two days ago!

Thanks :)

45| James said,

Thank you. This solved alot of frustrations for me!

46| Manuel said,

When you have xp + apache + php and need to connect to mssql showed me the error “Unable to connect to server:” after doing everything suggested, I looked around the pc where there was another Ntwdblib.dll library and found a directory c: \ php which I replaced with the correct version and I finally worked. Hopefully someone will serve.

47| mk33 said,

thank you, problem solved …

48| alleen said,

Thank you so much for posting this article. Now I can go to sleep because my frustration has been gone. Good Job! God Bless you bro for sharing your knowledge…

49| Savar said,

Thanks a lot the connection method worked excellently

I had to replace the ntwdblib.dll file in the apache bin folder, php folder and system 32 folder before it worked!!!

Thanks Again

50| PHP CMS said,

After several hours of web surfing, I finally got the solution. Thanks.

51| Alex said,

Thank, It helps me too

Leave a Comment
Name:
Email:
Website:
 
HOME | ABOUT US | CONTACT US | SITEMAP | GOOGLE SITE SEARCH | TOP
12 Lorcan Crescent, Santry, Dublin 9, Ireland +353 87 9807629