Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php 7 unable to initialize sqlsrv

I searched all day to find out a solution for sqlsrv dll on php 7 VC14 x64 Thread Safe and i did not found a solution. Does anyone solved this issue :

[04-Oct-2015 19:48:05 UTC] PHP Warning:  PHP Startup: pdo_sqlsrv: Unable to initialize module
Module compiled with module API=20131226
PHP    compiled with module API=20141001
These options need to match
 in Unknown on line 0

Here is my php7 RC4 informations :

System  Windows NT 6.0 build 6002 (Windows Server 2008 Standard Edition Service Pack 2) AMD64
Build Date  Sep 29 2015 17:15:28
Compiler    MSVC14 (Visual C++ 2015)
Architecture    x64
like image 356
Pierre-Luc Bolduc Avatar asked Oct 04 '15 21:10

Pierre-Luc Bolduc


People also ask

How do I enable Sqlsrv?

The SQLSRV extension is enabled by adding appropriate DLL file to your PHP extension directory and the corresponding entry to the php. ini file. The SQLSRV download comes with 8 driver files, four of which are for PDO support. The most recent version of the driver is available for download here: » SQLSRV download.

How enable mssql in php ini?

Go to the extracted folder and copy the NTS (for windows) dll files to the php extension folder. Once the files are copied, we need to add the following extension in php. ini file. Create a phpinfo file and check if the extensions are shown properly.


2 Answers

sqlsrv isn't ready for PHP7 yet but that's not true it wasn't released since 2012. Last release is from May 2015. Maintainer is Microsoft and version for PHP7 is planned on the beginning of next year.

https://github.com/Azure/msphpsql/issues/58

Update:

Driver is available for PHP up to 7.1 version even for Linux today (March 2017).

https://github.com/Microsoft/msphpsql

like image 95
kba Avatar answered Oct 11 '22 04:10

kba


For future reference (tested on Windows 7 with Xampp and PHP 7.0.13):

  1. Download and install ODBC drivers here: https://www.microsoft.com/en-US/download/details.aspx?id=36434

  2. Download the DLL here (both 7.0. and 7.1.can be found): https://github.com/Microsoft/msphpsql/releases

  3. Open your "php.ini" file and look for the "extension_dir" line. This will tell you where to put the DDL files. Note: On Xampp, it should be something like: "C:\xampp\php\ext"

enter image description here

  1. Put the DLL files contains in the Zip archive in your extension directory. Make sure to select the proper select the proper version. Note: I initially tried to use the x64 version, but it didn't work. Then, I replaced the DLL with the x86 version and it finally worked.

enter image description here

  1. Back in your "php.ini" file, you need to add the following line: "extension = php_pdo_sqlsrv_7_ts.dll". Note: Xampp use the tread safe version.

enter image description here

  1. Make sure to restart your apache service.

enter image description here

Code sample for testing:

$db = "the name of your database"
$password = "password";
$server = "IP address or named pipe";
$user = "username"

try {

    $connection = new PDO(
        "sqlsrv:Server=" . $server . 
            ";Database=" . $db, 
        $password, 
        $user
    );

    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $exception) {

    var_dump($exception);
}
like image 24
Jonathan Parent Lévesque Avatar answered Oct 11 '22 02:10

Jonathan Parent Lévesque