Install the SQL Server Driver for PHP

by Tali Smith

Introduction

The Microsoft Drivers for PHP for SQL Server are designed to enable reliable, scalable integration with SQL Server for PHP applications on Windows, Linux, and macOS platforms. The drivers are PHP extensions that allow reading from and writing to SQL Server databases in all editions of SQL Server (including Express editions) within PHP scripts. Two drivers are available: The SQLSRV driver provides a procedural interface for interacting with SQL Server. The PDO_SQLSRV driver implements PHP's object-oriented PDO interface for working with databases.

The drivers' application programming interface (API) includes support for Active Directory authentication, transactions, parameter binding, streaming, metadata access, Unicode data handling, and error handling. The drivers rely on the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. The drivers also support Microsoft SQL Azure Database, enabling developers to build PHP applications with relational capabilities using SQL Azure Database.

The Microsoft Drivers for PHP for SQL Server download is freely available to all SQL Server users. Releases and source code for the drivers are available from the Github project page. Full documentation for the drivers is available on Microsoft Docs; what follows is a brief overview of installation and configuration on Windows and IIS.

System Requirements

Full details on the system requirements for all supported versions of the driver can be found on the system requirements page. The following software is required:

  • On the client machine:
    • A supported Windows, Linux, or macOS operating system
    • A supported version of PHP
    • A supported version of the Microsoft ODBC Driver for SQL Server
    • A Web server configured to run PHP
  • On the server:
    • A supported version of SQL Server or Azure SQL Database

Install the Drivers

The Microsoft Drivers for PHP for SQL Server can be installed using the Web Platform Installer. To download and install the drivers manually instead, perform the following steps:

  1. Download and run the installation package from the appropriate link on the download page.

  2. Enter a directory to extract the package into when prompted.

  3. Open the directory you chose in step 3 and copy the required dll libraries to your PHP extension directory. The filenames indicate which PHP version, threadedness, and architecture each dll file is for. For example, php_sqlsrv_73_ts_x64.dll is the 64-bit SQLSRV driver for thread-safe (ts) PHP 7.3.

  4. Download and install the Microsoft ODBC Driver for SQL Server from the appropriate link on the ODBC download page.

  5. Edit your php.ini file to add the following lines in the Extensions section:

    Extension=php_sqlsrv.dll
    

    Substitute the names of the files you copied to your PHP extension directory as required. Full instructions for loading the drivers can be found here.

  6. Start a command prompt as administrator and run iisreset to restart your IIS server.

Configure the Drivers

  1. Retrieve the current configuration settings for the SQLSRV driver using the sqlsrv_get_config function, or for the PDO_SQLSRV driver using the PDO::getAttribute function.

  2. Change the configuration settings for the SQLSRV driver using the sqlsrv_configure function, or for the PDO_SQLSRV driver using the PDO::setAttribute function.

  3. Ensure that the drivers are 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 the IIS root directory.

    3. Open a browser, and go to http://localhost/info.php.

    4. Scroll down the resulting page to find the sqlsrv and pdo_sqlsrv sections. Confirm that the drivers are 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

When using the SQLSRV driver, the sqlsrv_connect() function is used to establish a connection to the server. When using the PDO_SQLSRV driver, the PDO::__construct function is used to establish a connection to the server.

Connections can be made using Windows authentication, SQL Server authentication, or Azure Active Directory authentication. The default is to use Windows authentication. 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.