Connecting php 7.2 to MS SQL using sqlsrv

Daves picture Daves · Dec 28, 2017 · Viewed 60.5k times · Source

I'm trying to get a connection to MS SQL up and running via PHP on my machine. I'm running IIS, have PHP 7.2 installed and MS SQL Express 2017. I have my basic web page running but when I click to open the PHP page, the connection does not work.

session_start();

echo "Hello ";
if (isset($_POST['submit'])) {
    $_SESSION["server"] = $_POST['server'];
    $_SESSION["database"]= $_POST['database'];
    $_SESSION["username"] = $_POST['username'];
    $_SESSION["password"] = $_POST['password'];

    echo $_SESSION["database"];

    //CONNECTION
    $serverName = $_SESSION["server"];
    $connectionInfo["Database"] = $_SESSION["database"];
    $connectionInfo["UID"] = $_SESSION["username"];
    $connectionInfo["PWD"] = $_SESSION["password"];

    echo "midway";

    $conn = sqlsrv_connect($serverName, $connectionInfo);

    echo "Bye";
}

When I run this I get "Hello dimensions midway" which suggests the page is working until it hits the connection line.
I am not sure what sqlsrv extension to use.
I have looked around and can see versions for 7 and 7.1 but not for 7.2.

I have added in extension=php_sqlsrv_71_nts_x86.dll to the bottom of php.ini (and the file exists in C:\Program Files (x86)\PHP\v7.2\ext).

Any pointers would be gratefully received. I've spent a couple of days on this and don't think I'm getting anywhere.
Thanks

Answer

Crennotech picture Crennotech · May 18, 2018

I faced the same issue. Later I found that I was using older version of Microsoft SQL files for PHP.

For PHP 7.2 you have to use 7.2 version of MS PHP SQL

That version is not available through microsoft website. I found it here https://github.com/Microsoft/msphpsql/releases

Once I used those files everything works fine. I used 64 bit files.

Then you need to add these lines in the php.ini file

extension=php_sqlsrv_72_nts.dll
extension=php_pdo_sqlsrv_72_nts.dll