Using PHP 5.3 with MSSQL PDO on Windows
Due to some legacy applications at the office, I occasionally find myself needing to connect to MSSQL through PHP for some various tasks. Unfortunately as of version 5.3, PHP stopped shipping with a default mechanism to connect to a MSSQL database. Here’s a quick walkthrough on how to get a connection set up with PDO on Windows. (tested on windows 7 with SQL Server 2000)
You’ll need to download a couple installers from Microsoft:
After you run the installers you’ll need to choose the correct driver to add to your php.ini. In my case it was php_sqlsrv_53_ts_vc9.dll but your system may be different. For an explanation of the different driver use cases check “Driver Versions” on this MSDN page. Once you’ve selected the correct driver add it to your php.ini.
Remember to restart Apache for the changes to take affect.
You should now be able to connect to MSSQL through PDO with this:
$db_server = '127.0.0.1'; $db_database = 'test'; $db_user = 'username'; $db_passwd = 'password'; $db = new PDO("sqlsrv:server=$db_server;database=$db_database;", $db_user, $db_passwd);
On the plus side, you can now interact with MSSQL in an object oriented model rather than using the mssql_ family of functions.