使用 sqlsrv 将 php 7.2 连接到 MS SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48009654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:36:24  来源:igfitidea点击:

Connecting php 7.2 to MS SQL using sqlsrv

phpsql-serversqlsrvphp-7.2

提问by Daves

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.

我正在尝试在我的机器上通过 PHP 建立和运行 MS SQL 的连接。我正在运行 IIS,安装了 PHP 7.2 和 MS SQL Express 2017。我的基本网页正在运行,但是当我单击打开 PHP 页面时,连接不起作用。

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.

当我运行它时,我得到“Hello尺寸中途”,这表明页面正在工作,直到它碰到连接线。
我不确定要使用什么 sqlsrv 扩展。
我环顾四周,可以看到 7 和 7.1 的版本,但不能看到 7.2 的版本。

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

我已添加extension=php_sqlsrv_71_nts_x86.dll到 php.ini 的底部(该文件存在于 中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

任何指示将不胜感激。我已经在这方面花了几天时间,但我认为我没有任何进展。
谢谢

回答by Crennotech

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

我遇到了同样的问题。后来我发现我使用的是旧版本的 Microsoft SQL 文件用于 PHP。

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

对于 PHP 7.2,您必须使用 7.2 版本的 MS PHP SQL

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

该版本无法通过 microsoft 网站获得。我在这里找到它https://github.com/Microsoft/msphpsql/releases

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

一旦我使用了这些文件,一切正常。我使用了 64 位文件。

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

然后你需要在 php.ini 文件中添加这些行

extension=php_sqlsrv_72_nts.dll
extension=php_pdo_sqlsrv_72_nts.dll 

回答by Natta Wang

What do you need to determine are

你需要确定的是

  • Which version of PHP
  • The runtime you are using is Thread Safeor Non-Thread Safe
  • The runtime you are using is 64 bitsor 32 bits
  • 哪个版本的PHP
  • 您使用的运行时是线程安全的或非线程安全的
  • 您使用的运行时是64 位32 位

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.2which SQL Server extension version 5.3is the best fit.

检查您使用的 PHP 版本,所以更喜欢您的问题,这意味着PHP 7.2哪个SQL Server 扩展版本 5.3最适合。

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

然后转到下载页面,选择您喜欢的特定版本的链接。(在这个问题是5.3

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

您下载的文件将是一个可自解压的EXE文件,然后解压到您想要的位置。

you will see list of dllfiles like these...

你会看到这样的dll文件列表......

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_xxxand php_sqlsrv_xxx.

就像我之前说的,您需要确定选择正确的扩展文件。SQL Server 扩展需要 2 个名称以php_pdo_sqlsrv_xxx和开头的文件php_sqlsrv_xxx

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

下一个数字表示您的PHP版本,在本例中为PHP 7.2,因此您需要选择其中包含的文件名_72_

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

下一个数字表示您的运行时Thread SafeNon-Thread Safe,具体在文件名_ts_(Thread Safe) 或_nts_(Non-Thread Safe) 中。

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

下一个数字表示您的运行时32 bits64 bits,具体在文件名_x86(32 bits) 或_x64(64 bits)

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

所以检查你的 PHP 环境phpinfo(),或者php --version在你的控制台上运行命令,然后注意显示的消息,对于我的电脑,它看起来像这些......

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, ZTSmean to Thread Safe(Non-Thread Safewill be NTS), and the x86is 32 bits.

从这些信息来看,我得到了 PHP 7.2ZTS意味着线程安全非线程安全将是NTS),并且x8632 位

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

所以,我必须选择的扩展名是php_sqlsrv_72_ts_x86.dllphp_pdo_sqlsrv_72_ts_x86.dll

Final: copy them to your /php/extfolder and edit php.iniwith those 2 files name.

最终:将它们复制到您的/php/ext文件夹并php.ini使用这两个文件名进行编辑。

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

Then save php.inifile and then restart your server.

然后保存php.ini文件,然后重新启动服务器。

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

通过查看页面中phpinfo.php必须包含的pdo_sqlsrv来测试您的结果。这意味着您的环境可以运行 PHP 并立即连接SQL Server

回答by Let's Yo

I solved it ~ PHP Version 7.2.4

我解决了~ PHP 7.2.4版

pdo_sqlsrv : 5.3.0+11108

pdo_sqlsrv:5.3.0+11108

1.download the proper version sqlsrv and sqlsrv pdo

1.下载正确版本的sqlsrv和sqlsrv pdo

2.put it into XAMPP\PHP\ext folder

2.放入XAMPP\PHP\ext文件夹

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

3.之后将这一行写入 php.ini > module setting

extension=php_pdo_sqlsrv_72_ts.dll

扩展名=php_pdo_sqlsrv_72_ts.dll

extension=php_sqlsrv_72_ts.dll

扩展名=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

4.让我们测试一下MSSQL仍然有错误提示建议您下载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-2017then download the proper file

5.转到https://docs.microsoft.com/zh-tw/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-2017然后下载正确的文件

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

(我的是 SQL Server 的命令行实用程序 11?X86)

6.fire the test.php again everything works!

6.fire test.php 再次一切正常!

回答by Ravi Sigdel

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

我终于找到了解决方案。您所要做的就是在php.ini文件中使用这些扩展名。

php_sqlsrv_72_ts_x86.dll

php_sqlsrv_72_ts_x86.dll

php_pdo_sqlsrv_72_ts_x86.dll

php_pdo_sqlsrv_72_ts_x86.dll

and restart your XAMPP.

并重新启动您的 XAMPP。

P.S. "I am using PHP 7.2.5 "

PS“我使用的是 PHP 7.2.5”

回答by Kamlesh

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

如何在 xampp 上使用带有 PHP 7.2 版本的 SQL Server DLL 文件

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

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.

要从 PECL 存储库下载 PHP 7.1 或更高版本的 Windows DLL,请访问 SQLSRV 或 PDO_SQLSRV PECL 页面。

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

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

https://pecl.php.net/package/pdo_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.

注意:在 php.ini 文件中添加扩展 dll 文件名时,不要忘记删除 'php_' 前缀。例如。

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

extension=pdo_sqlsrv extension=sqlsrv 不要更改您放在 /xampp/php/ext/ 目录中的实际文件名。

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

“7.2 线程安全 (TS) x86”适用于我的 'sqlsrv' 和 'pdo_sqlsrv' 扩展。

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

重新启动 xampp 或 apache,然后您可以在测试 php 文件中打印 phpinfo() 时看到启用的“pdo_sqlsrv”扩展

enter image description here

在此处输入图片说明