IIS Tutorial Series (Part 2): Talking to SQL Server with PHP on IIS
Note: This article is Part 2 of the IIS Tutorial Series.
Starting with a little history, before PHP 5.3, the options of getting PHP to talk to a Microsoft SQL Database Server was either using a generic database connector, such as ODBC, or PHP’s included MSSQL connector extension. The MSSQL extension was nice compared to ODBC as you didn’t have to set up a separate object (the ODBC connection object) to talk to the SQL Server. The MSSQL extension is very similar to PHP’s popular MySQL Extension as there are functions solely for that database type. Disappointingly, beginning from PHP 5.3, that MSSQL has been deprecated and no longer included with the PHP package, as it was stated to be unstable and unofficially supported (as well as maintenance stopped a long time ago). Initially, people started re-compiling PHP 5.3 on their own with the old MSSQL to get the functionality back, but like for the most of us, re-compiling PHP is not something we would want to step into.
Fortunately, Microsoft stepped up their game and finally started to support more open-source projects such as PHP, and created an official driver to talk to their SQL Server with PHP. In this tutorial, I will go over how to install the SQL Server Driver, and connect to an SQL Server with PHP. This tutorial is applicable for SQL Server 2000 or later, using PHP versions 5.2.4 or later.
Note: You’ll need to have PHP installed on the IIS Server. If not, read Part 1 of the tutorial series on how to do so.
First, visit Microsoft’s page to download the SQL Server Driver for PHP. As of this writing, the driver is currently version 2.0. If your SQL Server is installed remotely (not on the same server as the IIS server), you’ll need to download the Microsoft SQL Server 2008 Native Client at Microsoft’s SQL Server 2008 Feature Pack site. If the SQL Server is on the same server, then you’ll already have the necessary Native Client library installed, and you can skip the following step. There are a lot of packages on that page. To quickly locate the right one, scroll down until you find a section “Microsoft SQL Server 2008 Native Client”, or you can use your web browser’s page search functionality to jump to it immediately. Download MSI package appropriate for your server.
If you had to download the SQL Server Native Client, run that executable to install it first. Then, execute the downloaded file for the SQL Server Driver. This will just do a self-extract to a folder. Save the extracted folder anywhere you want. In the folder will be a set of DLLs. Choose the DLL that is appropriate for the PHP package you are using. For more information on which DLL to use, look at the readme that’s also included in the folder. Copy that DLL and paste it into the “ext” directory of your PHP folder (e.g. C:\php\ext\). This adds the extension for PHP to use, and contains the SQL Server API functions you use in your code. This extension talks to the Native Client, which is the interface to the SQL Server.
Next, open up the php.ini in your PHP installation folder, and enable that extension. There should be a section with many lines of:
Add a new line that is specific for the DLL you are using. e.g. extension=php_sqlsrv_53_nts_vc9.dll
Restart the IIS Server for all the settings to take effect. To check that the extensions is working, create a PHP script on your website with phpinfo and the section for the “sqlsrv” extension should be in there somewhere.
That should be it for the installation. In the folder for the SQL Server driver should also be a file named SQLSRV20_Help.chm, which is a help file that includes the API documentation of the driver. There is an online version of the documentation as well.
For example, to connect to a SQL Server database with PHP, you can use something similar to the following code:
$connectionInfo = array('UID'=>'Username', 'PWD'=>'password', 'Database'=>'Northwind'); $conn = sqlsrv_connect('MY-DBSERVER', $connectionInfo); if ($conn === false) die( print_r( sqlsrv_errors() ) );
Note: This code applies to authenticating as a SQL Server user, and not a Windows Authentication user. I have not gotten successful connection as a Windows user. If anyone knows how, please leave a comment.