Install the SQL Server Driver for PHP
by Tali Smith
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.
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:
Download and run the installation package from the appropriate link on the download page.
Enter a directory to extract the package into when prompted.
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.dllis the 64-bit SQLSRV driver for thread-safe (ts) PHP 7.3.
Download and install the Microsoft ODBC Driver for SQL Server from the appropriate link on the ODBC download page.
php.inifile to add the following lines in the
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.
Start a command prompt as administrator and run
iisresetto restart your IIS server.
Configure the Drivers
Ensure that the drivers are 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.phpin the IIS root directory.
Open a browser, and go to
Scroll down the resulting page to find the
pdo_sqlsrvsections. 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.
Links for Further Information
- Microsoft Drivers for PHP for SQL Server
- Download the Microsoft Drivers for PHP for SQL Server
- Overview of the Microsoft Drivers for PHP for SQL Server
- Programming Guide
- SQLSRV Example Application
- PDO_SQLSRV Example Application
- PHP Homepage
- Whitepaper: Accessing SQL Server Databases with PHP
- Drivers for SQL Server Team Blog
- SQL Azure Team Blog