One day, I found myself needing to use MSSQL Server Procs, using PHP 5.3 in a Linux environment. I soon found out that this was easier said than done as PHP 5.3 had removed built-in MSSQL support from the linux distribution and a proper install guide was far more common for Windows PHP users and PHP 5.2.
My first solution was to use FreeTDS, Linux ODBC and PDO ODBC. Out of a myriad voodoo style solutions, this one seemed the most straight forward. Unfortunately, there were issues. Exceptions never really worked right (caused the PHP module to die), NULL values were essentially unsupported, and if any string took more than 255 characters, PHP would demand 4GB of RAM.
Due to the short deadline of the project, crazed hacks were implemented, but I vowed to solve the issue properly at a later date.
So, after having long since conquered the beast, I present the ultimate guide, cradle to grave.
First step is a proper driver for MSSQL that works in Linux. The best and officially PHP supported is FreeTDS at http://www.freetds.org/
This guide assumes freetds-0.91.
Once you have it:
tar xzf freetds.tar.gz
cd freetds-0.91
./configure --with-tdsver=7.1 --enable-msdblib --with-gnu-ld && make
sudo make install
sudo /sbin/ldconfig
Once installed, you can do a basic connectivity test.
/usr/local/bin/tsql -S host -U username
You will be prompted for password. If you get a prompt, all is well.
Now for PHP, assuming PHP 5.3.8.
./configure line should have whatever your environment needs, but the key addition is the following:
--with-pdo-dblib=/usr/local
The libraries from FreeTDS by default go there in Centos, so that is where PHP needs to look.
Once PHP is all installed, we can do a basic test that things are working with a quick script.
<?php
$dbh= new PDO('dblib:host=hostserverhere;dbname=databasename;appname=MySweetApp', 'username', 'password');
$stmt = $dbh->prepare("SELECT SYSDATETIME()");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
?>
Run that and if you get output, congrats, you have succeeded.
That should be enough to get you started. Next edition, I share some PDO recipes to help the non-MSSQL familiar.