Skip to main content

How to install Microsoft’s SQL Server Driver for PHP

In this article I’ll show you how to install the Microsoft SQL Server Driver for PHP 5.6 and PHP 7. This makes the use of an SQL Server database back-end for your PHP website easy. If you want to communicatie with SQLServer from PHP you need to rely on some additional software and PHP extensions. This post walks you through the steps necessary to install the SQL Server driver and SQLSRV extension for PHP 5.6 and PHP 7, on Windows Server IIS of course.

How to install Microsoft’s SQL Server Driver for PHP 7

To let PHP communicate with an SQL Server database you need additional software: ODBC drivers and the PHP SQLSRV extension. You have to use the SQLSRV API functions to connect to an MS SQL Server database from PHP.

First you need to download all of this.

Required software to install PHP 7, 7.1 and SQL Server SQLSRV PHP extension

You need the following software, go ahead and download:

Hint: On Windows, download the DLLs from SQLSRV or PDO_SQLSRV PECL repository (source).

I thought you might find this interesting:   Ghost on IIS with HTTPS, how to resolve a "Too many redirects" error

SQL Server Native Client (SNAC) 2012

Since the release of ODBC Driver 11 for SQL Server you no longer have to install SQL Server Native Client – or SNAC – 2012. The Microsoft SQLNCli team blog states:

Microsoft is adopting ODBC as the de-facto standard for native access to SQL Server and Windows Azure SQL Database. We have provided longstanding support for ODBC on Windows and, in the SQL Server 2012 timeframe, released support for ODBC on Linux (Red Hat Enterprise Linux 5 and 6, and SUSE Enterprise Linux). Microsoft is aligning and renaming the previous ODBC drivers as follows:

  • SQL Server Native Client (SNAC) along with its ODBC functionality is now called Microsoft ODBC Driver 11 for SQL Server on Windows
  • Microsoft SQL Server ODBC Driver 1.0 for Linux along with its functionality is now called Microsoft ODBC Driver 11 for SQL Server on Linux

This change will result in our ODBC drivers for Windows and Linux being better aligned going forward. We hope that this change makes it easier for you to find the right driver for your needs and makes it easier to stay informed of new releases. Today’s availability marks the first combined release for your use.

And the Database Engine Developer Documentation states:

There are no more updates to the ODBC driver in SQL Server Native Client. The successor to the ODBC driver in SQL Server Native Client, called the Microsoft ODBC Driver 11 for SQL Server on Windows, is installed with SQL Server 2014. For more information about the Microsoft ODBC Driver 11 for SQL Server on Windows, see Microsoft ODBC Driver 11 for SQL Server – Windows

Visual Studio C++ 2015 for PHP 7 on Windows Server IIS

To install PHP 7, you need to have Visual Studio C++ 2015, or VC14, installed too. Download and install Visual C++ Redistributable for Visual Studio 2015 if you haven’t done so.

After you’ve downloaded and extracted the software, you need to install it all.

I thought you might find this interesting:   Simple PowerShell introduction for Windows Server administration, automation and scripting

Install ODBC Driver 11 for SQL Server
To install the ODBC Driver, in your cmd command or PowerShell prompt, simply type:

msiexec /i c:\path\to\msodbcsql.msi /quiet

Install Microsoft Drivers for PHP 7 for SQL Server
This isn’t really an installation, just download the msphpsql-PHP-7.0 zipfile from Github and unzip it. Within the archive you’ll find the folder msphpsql-PHP-7.0/binaries/<x64|x86>/ and two .dll files: php_sqlsrv_7_nts.dll and php_sqlsrv_7_ts.dll.

Pick the one you need for your IIS hosting environment, and copy the file to your PHP ext directory:

unzip msphpsql-PHP-7.0.zip -d msphpsql-temp
copy msphpsql-temp/msphpsql-PHP-7.0/binaries/x86/php_sqlsrv_7_nts.dll c:\php7\ext

To enable the SQLSRV extension, add to your php.ini file:

extension=php_sqlsrv_7_nts.dll

Install Visual Studio C++ 2015
If you haven’t installed Visual C++ Redistributable for Visual Studio 2015 yet, install both .msi files. PowerShell example:

Start-Process -NoNewWindow c:\path\to\vs2015\vc_redist.x86.exe -argument "/install /quiet" -Wait
Start-Process -NoNewWindow c:\path\to\vs2015\vc_redist.x64.exe -argument "/install /quiet" -Wait

Once everything is installed, the SQLSRV extension for PHP to SQL Server communication is available to your PHP scripts. We need to verify everything is in working condition.

How to test SQLSRV extension and ODBC Driver 11 for SQL Server with PHP

The best way (in my opinion) for you to test whether everything in PHP works correctly is on the command line. Really, executing PHP on the prompt is easier than browsing to your phpinfo() script (of course, you’re free to do so – verify the extension is loaded).

Just run php-cgi.exe with some parameters, like -v to print out the version:

C:\php7>php-cgi.exe -v
PHP 7.0.5 (cgi-fcgi) (built: Mar 30 2016 10:15:31)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

If something is wrong in your PHP configuration, an application crash notification pops up.

Got WinCache installed and available through a second php.ini file and FastCgi handler (you followed my how to install PHP and Wincache on IIS article ;-) ), test that one too:

C:\php7>php-cgi.exe -c php.wincache.ini -v
PHP 7.0.5 (cgi-fcgi) (built: Mar 30 2016 10:15:31)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

SQLSRV for PHP 7 test script: How To Connect to an SQL Server from PHP
Verify everything is working correctly with a test script. I always use a script like the one below to verify the PHP connection with my SQL Server database, and that it’s all working:

<?php
/*
 * Follow me on Twitter: @HertogJanR
 * Please donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=J24FGGU96YSUY
 */

$connectionInfo = array( "UID" => "user", "PWD" => "password", "Database" => "database" );
$link = sqlsrv_connect( "host name", $connectionInfo );
if( $link ) {
     echo "Connection established.<br />";
} else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true ) );
}
$sql = "SELECT table_name FROM information_schema.tables";

$stmt = sqlsrv_query( $link, $sql );
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) ) {
  echo $row['table_name']."<br />";
}

if( $stmt === false ) {
  die( print_r( sqlsrv_errors(), true));
}
?>

If everything went oké, all available SQL Server tables in your SQL database are printed.

I thought you might find this interesting:   How to fix a Joomla! HTTP 500 error after updates

SQLSRV for PHP 5.6

And what about the SQL Server driver – SQLSRV – for PHP 5.6? To install the Microsoft Drivers for PHP for SQL Server (ODBC Driver 11 for SQL Server) and the SQLSRV PHP extension, you can follow the same steps as for PHP 7.

For PHP 5.6 you need Visual C++ Redistributable for Visual Studio 2012 (Update 4) and the SQLSRV extension from the master branch: https://github.com/Azure/msphpsql. Download the appropriate Microsoft Drivers for PHP for SQL Server here:

https://www.microsoft.com/en-us/download/details.aspx?id=20098

Version support for PHP is as follows

  • Version 3.2 supports PHP 5.6, 5.5, and 5.4
  • Version 3.1 supports PHP 5.5 and 5.4
  • Version 3.0 supports PHP 5.4.

And check out Microsoft Drivers for PHP for SQL Server System Requirements.

Please share this post if you found it useful, thank you! If you have a valuable tip, please let me know and drop me a comment.

Jan Reilink

My name is Jan. I am not a hacker, coder, developer, programmer or guru. I am merely a system administrator, doing my daily thing at Vevida in the Netherlands. With over 15 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization.

Hi! Join the discussion, leave a reply!