How to install Microsoft's SQL Server Driver for PHP

There are many questions on Microsoft's SQL Server driver for PHP forum from people that cannot work out how to install the PHP driver.

There is a page on the PHP site with help on choosing the right extension build:
http://php.net/manual/en/install.pecl.windows.php, and general help is here: http://php.net/manual/en/install.windows.extensions.php.

Here are some explicit instructions which I hope can help new users of PHP.  After a while all of this will become common sense.

Instruction notes:

  1. Microsoft's PHP driver for SQL Server only runs with PHP 32-bit builds (as standard from http://windows.php.net/download).
  2. These instructions are only for PHP 5.2, 5.3 or 5.4. 
  3. I am pretending that PHP is installed into folder C:\PHP.  Replace the term C:\PHP in the instructions with whatever folder your PHP is installed into.
Instructions:
  1. Download Microsoft's PHP drivers.  Open page: http://www.microsoft.com/en-us/download/details.aspx?id=20098
    1. If you have Windows Vista, Server 2008 or above, download SQLSRV30.EXE.
    2. If you have Server 2003/Windows XP or below, download SQLSRV20.EXE
    3. If you cannot find SQLSRV20.EXE and would like to try a custom build of driver version 3.0 for Windows XP/2003...
      1. See this post: Unofficial php_sqlsrv 3.0 and download the rar archive
      2. Change step (2.) below - the driver files are in the 'bin' sub-folder in the archive. just extract the contents of the 'bin' folder to your extensions folder.
      3. When you extract the files, rename them as follows in order to work with the rest of these instructions:
        php_sqlsrv_30_54_ts_vc9x86.dll => php_sqlsrv_54_ts.dll
        php_sqlsrv_30_54_nts_vc9x86.dll => php_sqlsrv_54_nts.dll
        php_pdo_sqlsrv_30_54_ts_vc9x86.dll => php_pdo_sqlsrv_54_ts.dll
        php_pdo_sqlsrv_30_54_nts_vc9x86.dll => php_pdo_sqlsrv_54_nts.dll
        php_sqlsrv_30_53_ts_vc9x86.dll => php_sqlsrv_53_ts.dll
        php_sqlsrv_30_53_nts_vc9x86.dll => php_sqlsrv_53_nts.dll
        php_pdo_sqlsrv_30_53_ts_vc9x86.dll => php_pdo_sqlsrv_53_ts.dll
        php_pdo_sqlsrv_30_53_nts_vc9x86.dll => php_pdo_sqlsrv_53_nts.dll
      4. Change step (3.) below - if you cannot install (3.1) SNAC version 2012, install from (3.2) version 2008r2 - either will work.
      5. Assume for step (6.) below that you downloaded SQLSRV30.EXE.
         
  2. Extract the entire contents of the file downloaded in (1.) to your PHP extensions folder.  This is usually a sub-folder of the location where php is installed, named 'ext', e.g. C:\PHP\ext.
    Note that you can use WinRAR to open the .exe file and extract the files, if you want.
     
  3. Download the SQL Server Native Client (SNAC), the PHP driver depends on it:
    1.  For SQLSRV30.EXE, go here: http://www.microsoft.com/en-us/download/details.aspx?id=29065#SNAC
    2. For SQLSRV20.EXE, go here: http://www.microsoft.com/en-us/download/details.aspx?id=16978#SNAC
    Note:
    1. If PHP will run on 64-bit Windows, download the X64 Package.
    2. If PHP will run on 32-bit Windows, download the X86 Package.
       
  4. Install the SQL Server Native Client downloaded from the previous step (3.) on the same computer that runs PHP.
     
  5. Find out which version of PHP you have:
    1. Open a command window (run cmd.exe)
    2. Using your PHP folder, run command: "C:\PHP\php.exe" -i | more
      'more' loads the information page by page.
    3. The top line will show PHP Version => 5.x.x...  Make a note of the version.  It will start with 5.4, 5.3 or 5.2.  The final (3rd) number is not important.

      For the next steps you may need to scroll to the 2nd information page in the console window, by pressing a key...
    4. For version 5.4 and 5.3, a little further down is setting PHP Extension Build => API... It will end with either ...TS,VCn or ...NTS,VCn.  Make a note of whether it says TS or NTS.  Also make a note of whether the final part is VC9 or VC6 (VC6 is still possible with PHP 5.3).
    5. For version 5.2, note the setting that says "Thread Safety enabled" or "Thread Safety disabled". 
      "Thread Safety enabled" means TS,VC6
      "Thread Safety disabled" means NTS,VC6.
       
  6. Edit your php configuration file, php.ini.  Go to the end of the file and add these lines, depending on your earlier choices.  You need to know your PHP version number (5.2, 5.3 or 5.4), and your TS/NTS setting, and whether you have a VC9 or VC6 build, all noted from step 5. above.

    You can calculate the files you need by looking at the file name and comparing to your PHP version, and remembering whether you downloaded SQLSRV20.exe or SQLSRV30.exe.  Here are all of the combinations that are possible:

    • For SQLSRV30.EXE, PHP 5.4, NTS, VC9 add lines:
      [PHP_SQLSRV]
      extension=php_sqlsrv_54_nts.dll
      [PHP_PDO_SQLSRV]
      extension=php_pdo_sqlsrv_54_nts.dll
       
    • For SQLSRV30.EXE, PHP 5.4, TS, VC9 add lines:
      [PHP_SQLSRV]
      extension=php_sqlsrv_54_ts.dll
      [PHP_PDO_SQLSRV]
      extension=php_pdo_sqlsrv_54_ts.dll
       
    • For SQLSRV30.EXE, PHP 5.3, NTS, VC9 add lines:
      [PHP_SQLSRV]
      extension=php_sqlsrv_53_nts.dll
      [PHP_PDO_SQLSRV]
      extension=php_pdo_sqlsrv_53_nts.dll
       
    • For SQLSRV30.EXE, PHP 5.3, TS, VC9 add lines:
      [PHP_SQLSRV]
      extension=php_sqlsrv_53_nts.dll
      [PHP_PDO_SQLSRV]
      extension=php_pdo_sqlsrv_53_nts.dll
       
    • For SQLSRV20.EXE, PHP 5.3, NTS, VC9 add lines:
      [PHP_SQLSRV]
      extension=php_sqlsrv_53_nts_vc9.dll
      [PHP_PDO_SQLSRV]
      extension=php_pdo_sqlsrv_53_nts_vc9.dll
       
    • For SQLSRV20.EXE, PHP 5.3, NTS, VC6 add lines:
      [PHP_SQLSRV]
      extension=php_sqlsrv_53_nts_vc6.dll
      [PHP_PDO_SQLSRV]
      extension=php_pdo_sqlsrv_53_nts_vc6.dll
       
    • For SQLSRV20.EXE, PHP 5.3, TS, VC9 add lines:
      [PHP_SQLSRV]
      extension=php_sqlsrv_53_ts_vc9.dll
      [PHP_PDO_SQLSRV]
      extension=php_pdo_sqlsrv_53_ts_vc9.dll
       
    • For SQLSRV20.EXE, PHP 5.3, TS, VC6 add lines:
      [PHP_SQLSRV]
      extension=php_sqlsrv_53_ts_vc6.dll
      [PHP_PDO_SQLSRV]
      extension=php_pdo_sqlsrv_53_ts_vc6.dll
       
    • For SQLSRV20.EXE, PHP 5.2, NTS, VC6 add lines:
      [PHP_SQLSRV]
      extension=php_sqlsrv_52_nts_vc6.dll
      [PHP_PDO_SQLSRV]
      extension=php_pdo_sqlsrv_52_nts_vc6.dll
       
    • For SQLSRV20.EXE, PHP 5.2, TS, VC6 add lines:
      [PHP_SQLSRV]
      extension=php_sqlsrv_52_ts_vc6.dll
      [PHP_PDO_SQLSRV]
      extension=php_pdo_sqlsrv_52_ts_vc6.dll
       
  7. Restart your web server for the change to take effect.