Monthly Archives: April 2011

Connecting to Mssql database servers using PHP on Linux

I recently had the pleasure(!) of trying to get PHP on Debian working correctly with a Microsoft SQL server so that the data could be migrated from a Mssql instance into a Mysql one.

Previous to this attempt, the developers were using a Windows machine as a ‘broker’ between the two database. This setup was much too slow for importing and exporting large amounts of data, ┬áso we decided to cut out the middle man (the Windows machine) and do all the processing on a single server.

First I needed to install a few prerequisite packages:

user@computer:$ apt-get install unixodbc-dev
user@computer:$ apt-get install libmysqlclient15-dev

Next we need to download and uncompress the FreeTDS source code:

user@computer:$ wget ftp://ftp.linuxforum.hu/mirrors/frugalware/pub/frugalware/frugalware-testing/source/lib-extra/freetds/freetds-0.82.tar.gz

Next we use configure and install FreeTDS with the following options:

user@computer:$ ./configure --enable-msdblib --prefix=/usr/local/freetds --with-tdsver=7.0 --with-unixodbc=/usr
user@computer:$ make
user@computer:$ make install

Next we need to download and uncompress the PHP source code:

user@computer:$ wget http://us.php.net/get/php-5.3.6.tar.bz2/from/www.php.net/mirror

Next we use configure and install PHP with the following options:

user@computer:$ ./configure --with-mssql=/usr/local/freetds --with-mysql --with-mysqli
user@computer:$ make
user@computer:$ make install

Lastly we will need to create and install the mssql module for PHP:

user@computer:$ cd ext/mssql
user@computer:$ phpize
user@computer:$ ./configure --with-mssql=/usr/local/freetds
user@computer:$ make
user@computer:$ make install

Now you should be able to connect to any Microsoft SQL (and Mysql) server from PHP using the functions found here.