To enable SQL Server access from ubuntu 16.04:
* Run these commands:
```
sudo curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > mssql-release.list
sudo mv mssql-release.list /etc/apt/sources.list.d
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql unixodbc-dev
sudo apt-get install php7.1-dev
sudo apt-get install autoconf
sudo pecl install pdo_sqlsrv-4.1.6.1
```
* Confirm ODBC driver installation:
```
odbcinst -q -d -n "ODBC Driver 13 for SQL Server"
```
* Update the php.ini file: run` php --ini` to find the path to your php.ini file
* Add this line to /path/to/php.ini
```
extension=pdo_sqlsrv.so
```
* Run `php -i` to confirm SQLSRV support was added
* NOTE: extensions (on my computer) were installed here:
`/usr/local/lib/php/extensions/no-debug-non-zts-20160303/`
* Test user access directly on the Windows server running MSSQL:
```
sqlcmd -S ip_address -U username -P password -d database -q "select * from table_name"
```
* On the Windows server: open port 1433 in your firewall
* Install the FreeTds utils to test the connection:
```
sudo apt-get install freetds-bin
tsql -H mssql.host.ip.address -U username -P password -D database -p 1433
```
* Formulate the correct DSN (Data Source Name):
See: http://php.net/manual/en/ref.pdo-sqlsrv.connection.php
* Sample PHP Code:
```
<?php
// Database params
$tcp = '192.168.3.126';
$port = 1433;
$user = "test";
$password = "Password123";
$database = "test";
// Open connection
try {
// Database connect -- use one of the two statements below
$dsn = 'sqlsrv:Server=tcp:' . $tcp . ',' . $port . ';Database=' . $database;
$dbh = new PDO( $dsn, $user, $password, array());
// SQL prepare
$sql = "SELECT * FROM prospects";
$sth = $dbh->prepare($sql);
// Execute
$sth->execute();
// Fetch results
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row) {
$output = '<pre>';
$output .= implode("\t", array_keys($row)) . PHP_EOL;
$output .= implode("\t", $row) . PHP_EOL;
while ($row = $sth->fetch(PDO::FETCH_NUM)) {
$output .= implode("\t", $row) . PHP_EOL;
}
}
} catch (PDOException $e) {
$output .= $e->getMessage();
}
$output .= '</pre>';
echo $output;
```