I was asked to look at querying a MSSQL database from an Ubuntu server. After some digging, bad information and trial & error it turned out to be very straight forward.

This has been tested on Ubuntu 8.10 and Microsoft SQL Server 2000/2008.

From the Ubuntu server

sudo apt-get install php5-sybase php5-odbc freetds-common

Configure /etc/freetds/freetds.conf, edit the entry at the bottom to point to your SQL server.

Ubuntu Freetds Conf

Test connection to the MSSQL server with this PHP script

<?php

$server = 'servername';
$username = 'sa';
$password = 'password';
$database = 'xxx';
$connection = mssql_connect($server, $username, $password);

if($connection != FALSE)
{
echo "Connected to the database server OK<br />";
}
else
{
die("Couldn't connect");
}

if(mssql_select_db($database, $connection))
{
echo "Selected $database ok<br />";
}
else
{
die('Failed to select DB');
}

$query_result = mssql_query('SELECT @@VERSION');
$row = mssql_fetch_array($query_result);

if($row != FALSE)
{
echo "Version is {$row[0]}<br />";
}
mssql_free_result($query_result);
mssql_close($connection);

You can browse to the location of the PHP via your browser, or run php filenameyouchose.php from the cli. This should return the version of the SQL server and give you confirmation that it worked.

Connected To MSSQL