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:

apt-get install unixodbc-dev
apt-get install libmysqlclient15-dev

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

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:

./configure --enable-msdblib --prefix=/usr/local/freetds --with-tdsver=7.0 --with-unixodbc=/usr
make
make install

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

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:

./configure  --with-mssql=/usr/local/freetds --with-mysql --with-mysqli
make
make install

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

cd ext/mssql
phpize
./configure --with-mssql=/usr/local/freetds
make
make install

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