PHP connection to Microsoft SQL server from LAMP server

We have recently been working on a project for a client that involves dynamically creating web pages from data stored in a Microsoft SQL database. The solution is based on a LAMP (Linux, Apache, MySQL and PHP) server running Ubuntu.

Why not use IIS I hear you ask? Mainly because of costs and licensing and the Ubuntu server will be running several Web sites.

Here is a very basic example of the PHP code using PDO and DBLIB:

$user="username";
$password="password";
$dbh= new PDO('dblib:host=SERVERNAME\SQLSERVERINSTANCE;dbname=DBNAME',$user,$password);

$query = “SELECT * FROM SOME_TABLE”;
$statement = $dbh->prepare($query);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);

var_dump($result);

You need to install the DBLIB PDO driver:

sudo apt-get install php7.0-sybase