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.

System Requirements

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:

Install the SQL Server Driver for PHP

Perform the following steps to download and install the SQL Server Driver for PHP.

  1. Download %SQLServerDriverForPHP.EXE to a temporary directory.
  2. Double-click to run %SQLServerDriverForPHP.EXE.
  3. 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.
  4. Double-click to open the SQLServerDriverForPHP.chm help file.
  5. Install the Microsoft SQL Server Native Client from the link provided.
  6. Copy the php_sqlsrv.dll and php_sqlsrv_ts.dll files to the C:\PHP\ext\ directory.
  7. Open the C:\PHP\php.ini file, and add the following line in the Extensions section:

  8. Click Start, and select the search field.
  9. Type iisreset, and then press ENTER.

Configure the Driver

  1. Retrieve the driver's current configuration settings using the sqlsrv_get_config function.
  2. Change the configuration settings using the sqlsrv_configure function, or set values for the configuration settings in the php.ini file.
  3. Ensure that the driver is loaded and verify the configuration settings by running a script that calls the phpinfo() function:

    1. Open a text file, and copy the following code into it:

      <?php phpinfo(); ?>
    2. Save the file as info.php in your Web server's root directory.
    3. Open a browser, and go to http://localhost/info.php.
    4. 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.