##SQLSRV PHP Extension Installation##
SQLSRV is a PHP extension that enables connectivity to SQL Express Databases.
PHP versions up to 5.3 contained built-in support for MSSQL, but later versions have dropped support entirely. Microsoft developed its own official extension that supports PHP versions 5.3.6 to 5.4, but they have not updated their extension to work with later versions of PHP.
However, there is an unofficial version of this extension developed by a 3rd party which supports PHP version 5.3 to 5.6. This section explains how to install this extension on PHP 5.6. You can read more about it here:
Extract SQLSRV ZIP file
Extract the file sqlsrv_unofficial_3.0.2.2.zip
The archive contains many files, but we only need 2 of them, namely:
php_pdo_sqlsrv_56_nts.dll
php_sqlsrv_56_nts.dll
These files are designed for PHP 5.6.x NTS 32-bit. Copy the 2 files to your PHP installation Extension folder, usually located in:
C:\php\ext\
Now edit your php.ini file (C:\php\php.ini
) so that it knows to load the extension files:
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
; If you wish to have an extension loaded automatically, use the following
; syntax:
;
; extension=modulename.extension
;
; For example, on Windows:
;
; extension=msql.dll
;
; If you only provide the name of the extension, PHP will look for it in its
; default extension directory.
;
; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5)
; extension folders as well as the separate PECL DLL download (PHP 5).
; Be sure to appropriately set the extension_dir directive.
;
extension=php_sqlsrv_56_nts.dll
extension=php_pdo_sqlsrv_56_nts.dll
Install SQL Native Client
This installation is required for SQLSRV Extension to work. Simply run the msi installer.
sqlncli32.msi http://go.microsoft.com/fwlink/?LinkID=239647&clcid=0x409
sqlncli64.msi http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x409
Restart IIS to reload PHP
Now we need to reload PHP so that it can load our new extensions. To do this, simply restart IIS via the IIS Manager snap-in. Click on the root node on the left (your computer name), then on the far right, click Restart.
Test SQLSRV Extension
Create a new text file containing the following code, and save it as testsql.php
, in your root web folder which by default is:
C:\inetpub\wwwroot\
NOTE: you need to change the connection details at the top of the file!
<?php
/////////////////////////////////////////////
// DB connectivity info
/////////////////////////////////////////////
$mssql_dbhost='localhost\SQLEXPRESS';
$mssql_dbuser='sa';
$mssql_dbpass='sambapos';
$mssql_dbname='SambaPOS5';
$dbconn = msdbconnect();
echo $dbconn ? 'Connection successful!' : 'Connection FAILED!';
function msdbconnect () {
global $mssql_dbhost, $mssql_dbuser, $mssql_dbpass, $mssql_dbname;
$connectionInfo = array("UID"=>$mssql_dbuser,
"PWD"=>$mssql_dbpass,
"Database"=>$mssql_dbname, "ReturnDatesAsStrings"=>true);
$mssql_dbresource = sqlsrv_connect( $mssql_dbhost, $connectionInfo);
if( $mssql_dbresource === false ) {
echo '<b style="color:#FF0000">ERROR connecting to MSSQL:</b>';
echo '<PRE><CODE>';
print_r(sqlsrv_errors(), false);
echo '</CODE></PRE>';
die('<b style="color:#FF0000">EXECUTION TERMINATED!</b>');
}
Return $mssql_dbresource;
}
?>
Now open a Browser and navigate to http://localhost/testsql.php