Connecting PHP to MS SQL Server

Posted: 2014-09-27 22:26:28

php_sqlsrv.dll is PHP’s Database Driver for MS SQL Server, that has replaced php_mssql.dll (which no longer exists in PHP 5.3 and above).

While the php_sqlsrv API is not the same as the php_mssql API, php_sqlsrv can be used through php_pdo_sqlsrv, which provides the common PDO Database interface that can access MS SQL Server. php_sqlsrv can also be used directly (without the PDO abstraction).

There is 1 official build of php_sqlsrv.dll by Microsoft that is provided for PHP 5.3. And several unofficial builds of php_sqlsrv.dll for PHP 5.6, 5.5, 5.3 (32 bit and 64 bit).

PHP 5.5 Driver for MS SQL Server

Download the unofficial builds maintained here:
http://robsphp.blogspot.co.uk/2012/06/unofficial-microsoft-sql-server-driver.html

In the OneDrive folder, right-click on file “sqlsrv_unofficial_3.0.2.2.zip” and select “Download”.

Inside of file sqlsrv_unofficial_3.0.2.2.zip, extract these files out:

Into:
C:\WampDeveloper\Versions\Php\php-5.5.19.0-r1-win32-vc11-standard\ext\

Files:
php_pdo_sqlsrv_55_ts.dll
php_sqlsrv_55_ts.dll

Into:
C:\WampDeveloper\Versions\Php\php-5.5.19.0-r1-win32-vc11-standard-fcgi\ext\

Files:
php_pdo_sqlsrv_55_nts.dll
php_sqlsrv_55_nts.dll

* The above PHP paths are WampDeveloper Pro’s. If you are using another WAMP (such as Xampp or WampServer), just substitute in its own path.

Microsoft’s PHP 5.3 Driver for MS SQL Server

Download the official build maintained here:
http://www.microsoft.com/en-us/download/details.aspx?id=20098

Download file:
SQLSRV30.EXE

This file is not really an EXE (it’s a compressed CAB file), and if you try to run it Windows will display error message: “sqlsrv30.exe is not a valid win32 application”.

You will need to open (not run) SQLSRV30.EXE with either of these un/compression programs: WinRar, or 7-Zip (I use 7-Zip myself, it’s free and open source).

Inside of file SQLSRV30.EXE, extract these files out:

Into:
C:\WampDeveloper\Versions\Php\php-5.3.29.0-r2-win32-vc9-standard\ext\

Files:
php_pdo_sqlsrv_53_ts.dll
php_sqlsrv_53_ts.dll

Into:
C:\WampDeveloper\Versions\Php\php-5.3.29.0-r2-win32-vc9-standard-fcgi\ext\

Files:
php_pdo_sqlsrv_53_nts.dll
php_sqlsrv_53_nts.dll

* The above PHP versions are WampDeveloper Pro’s (v5.1.0.1) latest PHP 5.3 releases (regular PHP and FCGI-PHP).

Then via Components Tab, switch WampDeveloper from using Channel: Current (Apache 2.4 + PHP 5.5 + MySQL 5.6) to Stable (Apache 2.2 + PHP 5.3 + MySQL 5.5)…
http://www.devside.net/archives/wamp-server/howto-switch-wampdevelopers-apache-php-mysql-branches

Load php_sqlsrv.dll

Rename the files under all PHP versions to:
php_pdo_sqlsrv.dll
php_sqlsrv.dll

Edit file php.ini, and insert the loading of “php_sqlsrv.dll”…

[PHP_SQLSRV]
extension=php_sqlsrv.dll
extension=php_pdo_sqlsrv.dll

Save file. Restart Apache.

Links

“Microsoft Drivers for PHP for SQL Server” project
http://sqlsrvphp.codeplex.com/

“Update For PHP 5.5″ discussion
http://sqlsrvphp.codeplex.com/discussions/441706

“Data Developer Center SQL Server Driver for PHP” page
http://msdn.microsoft.com/en-us/data/ff657782.aspx

“Accessing SQL Server Databases from PHP”
http://social.technet.microsoft.com/wiki/contents/articles/1258.accessing-sql-server-databases-from-php.aspx