Jul 13

PHP with SQL Server Express and Windows 2003

Tag: System Administration, Windows, Web Development, PHPmunckfish @ 6:06 pm

I’ve just set up SQL Server Express on our staging server cause I needed to interface Moodle with a customers existing SQLServer database for authentication.

This wasn’t as simple as I thought it would be, but after some set backs I got it working :)

Firstly, TCP connections to SQL Server Express are switched off by default to prevent newbie users opening themselves up to potential remote attack. Here’s a link to instructions on how to enable TCP connections: How to: Configure Express to accept remote connections.

Also note from the comments on the PHP mssql_connect function documentation that the hostname and port in the connection string are delimited using a comma ‘,’ rather than the more common colon ‘:’. E.g.

  1.  
  2. mssql_connect(‘localhost,1433′, ‘myusername’, ‘mypass’);
  3.  

The first problem I encountered after this was an ‘Unable to connect to server’ error. By scanning the user comments on the PHP mssql_connect function page I noticed that a key step in fixing the problem was to replace the PHP5 provided ntwdblib.dll with a newer one from the MS SQL Client Tools. However, this DLL no longer ships with MS SQL Server Express. Luckily I had a developer installation of SQL Server 2000 on my XP partition and was able to pick up version 2000.80.2039.0 from that.

However, I then ran into another problem. When I replaced the PHP 5 provided version ntwdblib.dll of with the new one and restarted IIS, the output from phpinfo() showed that the mssql extension wasn’t loading anymore. I couldn’t see any errors reported in browser nor the system error logs.

By chance I decided to run php -i on the command line. This was lucky cause this way a system error dialog popped up a warning about a missing DLL dependency.

The problem was that the new ntwdblib.dll had a dependency on MSVCR71.DLL which couldn’t be found in IIS’ path. I did a search for msvcr*.dll in C:\WINDOWS and found a copy of this DLL in C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\. I copied it into my PHP install dir and restarted IIS. Then when I ran phpinfo() again the extension showed up.

4 Responses to “PHP with SQL Server Express and Windows 2003”

  1. John says:

    I know it was quite a while ago, but how/where did you replace ntwdblib.dll and register it?

  2. munckfish says:

    John, you should find ntwdblib.dll in the root of your PHP installation. E.g. if you installed PHP as C:\PHP you find it at C:\PHP\ntwdblib.php.

  3. cephas says:

    I am still having problem to set my php or to make it work with my SQL can some one tell me how

  4. munckfish says:

    cephas - if you need help like this the best thing to do is to sign up to the mailing lists on php.net and ask there.

    Or if you need more immediate help get onto IRC and join the channel ##php on freenode.net. There you will be able to ask for help directly, but you’ll need to be patient. A good IRC client for Windows is mIRC, or on Linux you could XChat.

Leave a Reply