php/PDO access to MS SQL Server from Ubuntu 16.04 LTS Server

The replacement of a Ubuntu 10.04 LTS server with one running Ubuntu 16.04 LTS necessitated moving from the php mssql_* functions to the PDO functions. This required the installation of the “dblib” PDO driver and FreeTDS. It took a fair bit of ferreting around to get it to work, so here’s a summary.

Installation of php 7.0 and php 5.6


sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php5.6 php7.0

Installation of modules
sudo apt-get install php5.6-mysql php-gettext php5.6-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0 php5.6-sybase freetds-common libsybdb5

Changing php versions

From php5.6 to php7.0:
sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart
sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php

from php7.0 to php5.6:
sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart
sudo ln -sfn /usr/bin/php5.6 /etc/alternatives/php

FreeTDS config

Example connection, retrieve SQL Server version, execute SELECT query, and display results

Leave a Reply