Connecting php 7.2 to MS SQL using sqlsrv

不羁岁月 提交于 2019-12-12 08:42:09

问题


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


回答1:


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 



回答2:


What do you need to determine are

  • Which version of PHP
  • The runtime you are using is Thread Safe or Non-Thread Safe
  • The runtime you are using is 64 bits or 32 bits

Then you must look at the requirement page here

Check the version of PHP you are using, so prefer to your question, it's mean to PHP 7.2 which SQL Server extension version 5.3 is the best fit.

Then go to download page, select the link which specific with version you prefer. (in this question is 5.3)

The file you downloaded will be a self-extractable EXE file, then extract to the place where you want.

you will see list of dll files like these...

php_pdo_sqlsrv_7_nts_x64.dll
php_pdo_sqlsrv_7_nts_x86.dll
php_pdo_sqlsrv_7_ts_x64.dll
php_pdo_sqlsrv_7_ts_x86.dll
...
php_sqlsrv_72_ts_x86.dll

What is the meaning that is in the name of these files?

Like I said before, what you need to determine to select the proper extension file. The SQL Server extension need 2 files of the name start with php_pdo_sqlsrv_xxx and php_sqlsrv_xxx.

Next number mean to your PHP version, in this case PHP 7.2, so you need to choose file name has _72_ in its.

Next number mean to your runtime Thread Safe or Non-Thread Safe, which specific in the file name _ts_ (Thread Safe) or _nts_ (Non-Thread Safe).

Next number mean to your runtime 32 bits or 64 bits, which specific in the file name _x86 (32 bits) or _x64 (64 bits)

So check your PHP environment by phpinfo(), or run the command php --version on your console, and then notice the message displayed, for my computer it look like these...

PHP 7.2.12 (cli) (built: Nov  8 2018 06:12:12) ( ZTS MSVC15 (Visual C++ 2017) x86 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

By look from those information, I got PHP 7.2, ZTS mean to Thread Safe (Non-Thread Safe will be NTS), and the x86 is 32 bits.

So, what I must pick is the extension which are php_sqlsrv_72_ts_x86.dll and php_pdo_sqlsrv_72_ts_x86.dll

Final: copy them to your /php/ext folder and edit php.ini with those 2 files name.

// example with my case
extension=php_pdo_sqlsrv_72_ts_x86
extension=php_sqlsrv_72_ts_x86

Then save php.ini file and then restart your server.

Test your result by looking at phpinfo.php, which MUST has pdo_sqlsrv in the page. That's mean your environment can run PHP and connect to SQL Server now.




回答3:


I solved it ~ PHP Version 7.2.4

pdo_sqlsrv : 5.3.0+11108

1.download the proper version sqlsrv and sqlsrv pdo

2.put it into XAMPP\PHP\ext folder

3.after that write the line into php.ini > module setting

extension=php_pdo_sqlsrv_72_ts.dll

extension=php_sqlsrv_72_ts.dll

4.let's make a test for MSSQL still there is a error msg for recommend you to download ODBC Driver for SQL

5.Go to https://docs.microsoft.com/zh-tw/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-2017 then download the proper file

(mine is Command Line Utilities 11 for SQL Server® X86)

6.fire the test.php again everything works!




回答4:


I finally found the solution. All you have to do is use these extensions in php.ini file.

php_sqlsrv_72_ts_x86.dll

php_pdo_sqlsrv_72_ts_x86.dll

and restart your XAMPP.

P.S. "I am using PHP 7.2.5 "




回答5:


How to use SQL Server DLL files with PHP 7.2 version on xampp

https://github.com/Microsoft/msphpsql/releases

To download Windows DLLs for PHP 7.1 or above from the PECL repository, please go to the SQLSRV or PDO_SQLSRV PECL page.

https://pecl.php.net/package/sqlsrv/5.6.1/windows

https://pecl.php.net/package/pdo_sqlsrv/5.6.1/windows

NOTE: Do not forget to remove 'php_' prefix when you add extension dll filename in php.ini file. eg.

extension=pdo_sqlsrv extension=sqlsrv Do not change actual filename which you put in /xampp/php/ext/ directory.

"7.2 Thread Safe (TS) x86" worked for me for 'sqlsrv' and 'pdo_sqlsrv' extensions.

Restart xampp or apache then you can see enabled 'pdo_sqlsrv' extension when you print phpinfo() in a test php file



来源:https://stackoverflow.com/questions/48009654/connecting-php-7-2-to-ms-sql-using-sqlsrv

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