If you are using PHP hopefully you are running on Linux which means you probably won’t have the unfortunate task of having to connect to MS SQL Server. Although rare, there may come a time where you need to integrate with an outside system that uses a SQL Server database and no other options for getting data are viable (ie APIs).

This post will outline how to set this up. In this case I was using Ubuntu 14.04 LTS 64 bit but the instructions should be pretty close to earlier versions or 32 bit. These are what worked for me on a clean Virtual Box installation then replicated on a remote server. It is possible some of the commands are redundant or not needed. Please comment if you find some. These instructions assume you already have the LAMP stack installed.

  1. Apt-Get Commands
    • sudo apt-get update
    • sudo apt-get install unixodbc tdsodbc php5-odbc freetds-dev unixodbc-dev freetds-bin
    • sudo apt-get update
    • sudo service apache2 restart
  2. /freetds/freetds.conf – Here you can declare multiple server parameters if you need to have multiple connections. In this case we are using the connection defined with ‘sqlsrv’
  3. /etc/odbcinst.ini
  4.  /etc/odbc.ini
  5. connect.php – PHP file to connect to database. This is just an example. You’d probably want to declare a database class and connect to the database as needed.

December 30th, 2015

Posted In: Uncategorized

  • Mohan Raj

    how can i use this on xampp + PHP 7+Ubuntu 12.04? Please help me

    • Hello Mohan,

      I am really not familiar with XAMPP. I don’t see any technical reason why it wouldn’t work but I am not familiar with installing PHP extensions that way.