Install the SQL Server Driver for PHP
by Tali Smith
The Microsoft® SQL Server® Driver for PHP is designed to enable reliable, scalable integration with SQL Server for PHP applications deployed on the Windows® platform. The driver is a PHP extension that allows the reading and writing of SQL Server data from within PHP scripts. It provides a procedural interface for accessing data in all editions of SQL Server(including Express editions) and makes use of PHP features, including PHP streams, to read and write large objects. The SQL Server Driver for PHP relies on the Microsoft SQL Server Native Client to communicate with SQL Server.
The SQL Server Driver for PHP download is available to all SQL Server users at no additional charge. The most current version of the driver, Microsoft SQL Server Driver for PHP 1.1, provides new capabilities for building PHP applications, and the driver supports Microsoft® SQL Azure™ Database and PHP version 5.3, enabling developers to build PHP applications with relational capabilities using the SQL Azure Database.SQL Server Driver for PHP 1.1 also includes support for UTF-8 and for scrollable results.
Microsoft provides support for the SQL Server Driver for PHP under its normal support methods. While the source code for this driver has been made available on the CodePlex Web site, Microsoft supports only the signed version of the driver from the Microsoft® Developer Network (MSDN®) download site.
The SQL Server Driver for PHP application programming interface (API) includes support for Windows authentication, transactions, parameter binding, streaming, metadata access, and error handling. The SQL Server Driver for PHP relies on the Microsoft® SQL Server® 2005 Open Database Connectivity (ODBC) driver to handle the low-level communication with SQL Server and is therefore only supported on the Windows operating system.
You need to ensure that the following are installed:
Supported operating systems:
- Windows Server® 2008 R2 or Windows Server® 2008
- Windows® 7, Windows Vista® Service Pack 1 (SP1), Windows® XP SP3
- Windows Server® 2003 SP2, Windows® 2000 SP4
- PHP 5.X.X. Microsoft SQL Server 2008 Native Client. You can download SQL Server Native Client from a Microsoft® SQL Server® 2008 Feature Pack, at: https://www.microsoft.com/downloads/details.aspx?familyid=B33D2C78-1059-4CE2-B80D-2343C099BCB4&displaylang=en%20
- Any edition of SQL Server 2005 or SQL Server 2008.
- A Web server configured to run PHP.
Install the SQL Server Driver for PHP
Perform the following steps to download and install the SQL Server Driver for PHP.
- Download %SQLServerDriverForPHP.EXE to a temporary directory.
- Double-click to run %SQLServerDriverForPHP.EXE.
- Enter an installation directory when prompted. It is recommended that you extract the file to
%ProgramFiles%with the default directory Microsoft SQL Server Driver for PHP.
- Double-click to open the SQLServerDriverForPHP.chm help file.
- Install the Microsoft SQL Server Native Client from the link provided.
- Copy the php_sqlsrv.dll and php_sqlsrv_ts.dll files to the
C:\PHP\php.inifile, and add the following line in the Extensions section:
- Click Start, and select the search field.
- Type iisreset, and then press ENTER.
Configure the Driver
- Retrieve the driver's current configuration settings using the sqlsrv_get_config function.
- Change the configuration settings using the sqlsrv_configure function, or set values for the configuration settings in the php.ini file.
Ensure that the driver is loaded and verify the configuration settings by running a script that calls the phpinfo() function:
Open a text file, and copy the following code into it:
<?php phpinfo(); ?>
- Save the file as info.php in your Web server's root directory.
- Open a browser, and go to http://localhost/info.php.
Scroll down the resulting page to find the sqlsrv section. Confirm that the driver is loaded and the configuration settings are set to the default values (see Figure 1).
Figure 1: SQL Server configuration settings page
Create a Connection to the Database
The sqlsrv_connect function is used to establish a connection to the server.
By default, the sqlsrv_connect function uses Windows authentication to establish a connection. In most scenarios, this means that the Web server's process identity or thread identity (if the Web server is using impersonation) is used to connect to the server, not a user's identity.
For more details on sqlsrv_connect, see: SQL Server Developer Center.
Links for Further Information
- PHP on Windows Training Kit (August 2009).
- Microsoft SQL Server Driver for PHP.
- SQL Azure Team Blog.
- SQL Server Driver for PHP 1.1 - October 2009.
- Microsoft SQL Server Driver for PHP team blog.
- New SQL Server Driver for PHP Whitepaper.
- SQL Server Driver for PHP example application.
- Accessing SQL Server Databases with PHP
- Programming Tasks.
- Introducing the SQL Server Driver for PHP.