MSSQL with PHP on Windows.

Ironic isn’t it? The MSSQL extension for PHP works better on Linux than it does on Windows.

Since the beginning of time, the MSSQL extension for PHP on Windows had a problem where empty varchar fields came through containing a space. The problem was caused by Microsoft’s own library returning the space to the PHP extension. Whether or not this was actually a bug is a different debate (Sybase docs make some strange statements that allude to, but don’t completely explain this behaviour) however what is certain is that it is a pain in the ass. Empty strings (”") are not ” “, which is not null!

So, this was transparently trimmed by PHP and happily passed to the script as an empty string, which was of course broken (some people actually want string values with 1 space in them). So this was corrected with the release of 4.3.4, which caused much screaming and gnashing of teeth.

Of course, while all this was happening, those users running PHP on Linux using FreeTDS were happily sitting back watching the fireworks as their library for interfacing with SQL Server was all good and showed empty strings for what they are, empty strings.

Thankfully, some bright spark at kromann.info had the idea of compiling the FreeTDS library as a DLL for use under Windows. Before you go and say that it a rather obvious fix, ask yourself when the last time you compiled PHP under Windows was!

Anyway, using it is a simple matter, just download php_dblib.dll for your version of PHP (the link is on the right under the PHP section of the site) and throw it in with the other extensions. Add it to your php.ini, setup a freetds.conf in your c:\ and you should be laughing. Note that you need to connect through a TDS DSN entry in the freetds.conf file as the server address, and the tds version must be at least 7 (”tds version = 7.0″).

I am extremely grateful that this library is available and replacing the antiquated php_mssql.dll with it will be the first thing I do when setting up a Windows dev environment. Check it out.

Leave a Reply