How to enable extension in PHP7.X for connecting to SQL Server

How to enable extension in PHP7.X for connecting to SQL Server
 
1 Introduction
It is required to connect to SQLServer(MSSQL) databases via PHP script on a Windows system.
This document describes how to enable extension for PHP 7.0.30.
 

2 Download SQL Server extension for PHP

The Microsoft Drivers for PHP for SQL Server require the following versions of PHP:
For PHP7.0.0+,We have to download Drive4.0 version or up.
We download Drive4.3(SQLSRV43.EXE) here as example. Drive4.3(SQLSRV43.EXE) contains below .dll files:
After decompression, select 32 or 64 bit extensions according to PHP version, and notice Thread safe(NTS and TS).
TS refers to Thread Safety, which is generally selected when IIS is loaded in ISAPI mode.
NTS refers to None-Thread Safe,which is generally selected when running in fast CGI mode, which has better performance.
 
3 Copy the files needed to the PHP extension directory
Put them in the corresponding version of PHP directory.
 (For example, use php_pdo_sqlsrv_7_nts_x86.dll and php_sqlsrv_7_nts_x86.dll).
 
4 Modify php.ini
Add the following two lines in “php.ini” to enable extensions:
extension=php_sqlsrv_7_nts_x86.dll
extension=php_pdo_sqlsrv_7_nts_x86.dll
  or      
 

 

5 Install ODBC Driver(if not installed)

ODBC Driver:
Microsoft® ODBC Driver 11 for SQL Server® - Windows (SQL Server® 2005):
Microsoft® ODBC Driver 13 for SQL Server® - Windows + Linux (SQL Server® 2016)
 
 
6 How to check if we have enabled SQL extension in PHP7.0.0+ or not
6.1 Create file “phpinfo.php” in the root directory of the website

6.2 Create file “test.php” in the root directory of the website

<?php
$serverName = "xxx.xxx.xxx.xxx"; //serverName\instanceName
$connectionInfo = array( "Database"=>"databasename_here", "UID"=>"username_here", "PWD"=>"password_here");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
 
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
Browser access http://xxx.xxx.xxx.xxx/test.php

Add Feedback