henning glatter-götz

Connecting to Microsoft SQL Server from PHP on OSX and XAMPP

UPDATE (March 23rd 2012):

I recently found a PHP binary that has a one-line installer. It is actively maintained by Liip AG. I now use that in combination with HomeBrew for all the rest. The Liip package is compiled with pdo_dblib which makes the problem described in this post a non-issue.

I recently found myself in the position of having to connect to a remote Microsoft SQL Server from my OSX system using PHP. The production environment runs Ubuntu LINUX, where connecting via mssql_connect() was no problem, but I develop on OSX and I could not get this to work initially.

mssql_connect() simply returns boolean FALSE on failure and PHP tells you little more than

1
Warning: mssql_connect(): Unable to connect to server: YOURSERVERNAME

It took quite a few hours to finally figure out that I could turn on some additional logging on the driver level. I use XAMPP and its PHP distribution uses FreeTDS for mssql access. There is a configuration file for this driver located at

1
/Applications/XAMPP/xamppfiles/etc/freetds.conf

that lets you turn on debug logging to a file.

Uncomment the following lines

1
2
;       dump file = /tmp/freetds.log
;       debug flags = 0xffff

And then run your connection attempt again. Take a look at the log for details on the failure.

In the end I added a new entry for an MS SQL server at the end of the freetds.conf file and used its name in the call to mssql_connect().

1
2
3
4
5
[MyServerXYZ]
        host = the_host_name
        instance = the_instance_name
        port = 1433
        tds version = 9.0

In my case I had to deal with an instance name that would normally be appended to the host name in a connection string, but for this driver configuration you have to use a variable called instance.

In PHP I then use this code during development

1
$conn = mssql_connect('MyServerXYZ', $username, $password);

Comments