Setup Apache2 with PHP 5.6 or 7 on windows with MS SQL module

A

Anonymous

Guest
Hi everyone,

Does anybody try to install and setup Microsoft SQL module for Apache on Windows?

I found couple tutorials on the web but when i open php info page sqlsrv module was not listed/loaded.
Code:
http://www.web-site-scripts.com/knowledge-base/article/AA-00748/0/PHP-drivers-for-Microsoft-SQL-Windows-Apache.html

I have small project and i need to read some data from SQL server instead of MySQL.

Thanks in advance!
 
I will post reply to my self :)

How to setup Microsoft SQL Extension for PHP and Apache on Windows

Download Microsoft Drivers for PHP for SQL Server from fallowing link:
Code:
https://docs.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017

Depends of the PHP version you use on the Apache web server, download properMicrosoft Driver version
Code:
https://docs.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-2017

PHP Version 7.2 > SQl Driver 5.2 (7.2.1+)
PHP Version 7.1 > SQl Driver 5.2 (7.1.0+) or 4.3
PHP Version 7.0 > SQl Driver 5.2 (7.0.0+) or 4.3 or 4.0
PHP Version 5.6 > SQl Driver 3.2 (5.6.4+)
PHP Version 5.5 > SQl Driver 5.2 (5.5.16++) or 3.1

Extract downloaded package

THIS IS REALY IMPORTANT!
On the phpinfo page, check row Architecture of the apache server.
If your architecture x86 then use _x86 dll's if architecture is x64 then use _x64 dll's (don't look your windows architecture!!)

[PHP 7.2 example] - for XAMPP webserver

Copy files php_pdo_sqlsrv_72_ts_x86.dll and php_sqlsrv_72_ts_x86.dll from extracted folders to c:\xampp\php\ext
then configure c:\xampp\php\php.ini file
enable extensions (or add lines if missing)

extension=php_pdo_sqlsrv_72_ts_x86.dll
extension=php_sqlsrv_72_ts_x86.dll

restart apache service.

Download and install Microsoft® ODBC Driver 13 for SQL Server (Windows) from fallowing link:
Code:
https://www.microsoft.com/en-us/download/details.aspx?id=50420

IMPORTANT!!!
Download architecture version of the ODBC driver according to your windows architecture x86 or x64 (not of the architecture of the Apache and PHP from the begining of this manual!)

English\X64\msodbcsql.msi 4.1 MB
English\X86\msodbcsql.msi 2.6 MB

After you install ODBC driver, go to your apache root, create file sql_serverinfo.php, edit it and put fallowing code:

Code:
<?php
$serverName = "DEMO-SQLSRV\SQLEXPRESS";
$conn = sqlsrv_connect( $serverName);
if( $conn === false ) {
     die( print_r( sqlsrv_errors(), true));
}

$server_info = sqlsrv_server_info( $conn);
if( $server_info )
{
    foreach( $server_info as $key => $value) {
       echo $key.": ".$value."<br />";
    }
} else {
      die( print_r( sqlsrv_errors(), true));
}
?>

Save file and open in in browser
Code:
http://localhost/sql_serverinfo.php

you should get returned values from MS SQL Server:

CurrentDatabase: master
SQLServerVersion: 12.00.4100
SQLServerName: DEMO-SQLSRV\SQLEXPRESS

And it's done!

You have successfully configured your apache and php on windows to connect on Microsoft SQL Server database.

To create queries use fallowing instructions from PHP
Code:
http://php.net/manual/en/book.sqlsrv.php

I hope this manual will be useful for someone.
 
Back
Top