php 7 unable to initialize sqlsrv

孤街浪徒 提交于 2019-12-05 03:57:00

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

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"

  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.

  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.

  1. Make sure to restart your apache service.

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);
}

For the record, the direct link to the sql server libraries is here

sqlsrvr for php 7

Is the 32 and 64 bits version both for ts and nts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!