Connecting PHP to MS SQL Server

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/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

8 thoughts on “Connecting PHP to MS SQL Server”

  1. Hello Sir,
    I m using php version 5.5.12 and i did all the task which you mentioned in this page but still i m getting “Call to undefined function sqlsrv_connect() ” error

    1. Check Apache’s and PHP’s error logs… Both the general and the website specific logs.

      It is likely that PHP was unable to load file php_sqlsrv.dll because the build between PHP and php_sqlsrv.dll are not matching…

      The php_sqlsrv.dll build has to be for:

      1. The same PHP branch:
      PHP 5.5

      2. The same PHP bit version:
      32 bit

      3. The same PHP run-type:
      TS (Thread Safe) or Regular – for PHP.
      NTS (Not Thread Safe) or FCGI – for PHP-FCGI.

    1. mssql_connect() is a function from the php_mssql.dll PHP extension.

      The php_mssql.dll PHP extension is only available under the older PHP 5.2 version.

      You can either:

      A. Switch to the PHP 5.2 branch (if using a WAMP such as WampDeveloper) and load php_mssql.dll in php.ini.

      B. Or you can try to locate all instances of mssql_ in your script, and update those lines to use the functions of the php_sqlsrv.dll driver (or to the database-neutral PDO interface).

  2. Hello,

    At first thank you for the awesome and useful page!

    I am using latest WAMP 2.5 32bit version under Win 8.1.

    Php version is 5.5.12. I am seeing that it has php5ts.dll in php folder. I did the regular install as per wamp website download.

    I am using the ts version, but when I am trying to connect I am getting this:

    Connection could not be established.
    Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft SQL Server 2012 Native Client, or SQL Server 2008 Native Client, SQL Server 2005 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft SQL Server 2012 Native Client, or SQL Server 2008 Native Client, SQL Server 2005 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) [1] => Array ( [0] => IM002 [SQLSTATE] => IM002 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified [message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified ) )

    Please, help!

    Thanks in advance!

  3. Hello,
    I am using latest WAMP 2.4 64bit version under Win 7.
    Php version is 5.4.16 ……
    i want to cennect SQL server 2005 to wamp server ….please help me in that…

    thanks

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>