由于 MSSQL 已弃用,我如何让 SQLSRV 扩展与 PHP 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7402713/
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:37:13  来源:igfitidea点击:

How do I get the SQLSRV extension to work with PHP, since MSSQL is deprecated?

phpsql-serverdll

提问by Gordon

I upgraded to PHP 5.3, and noticed that php_mssql.dll is missing. After googling a bit, it seems that 5.3 has dropped support for mssql. So I downloaded the drivers from microsoft, but I can't seem to get it to work.

我升级到 PHP 5.3,并注意到缺少 php_mssql.dll。在谷歌上搜索了一下之后,似乎 5.3 已经放弃了对 mssql 的支持。所以我从微软下载了驱动程序,但我似乎无法让它工作。

I am on windows. What do I need to do, after I unzip the files in the ext directory of my PHP 5.3?

我在窗户上。在我的 PHP 5.3 的 ext 目录中解压缩文件后,我需要做什么?

回答by Gordon

Quoting http://php.net/manual/en/intro.mssql.php:

引用http://php.net/manual/en/intro.mssql.php

The MSSQL extension is not available anymore on Windows with PHP 5.3 or later. SQLSRV, an alternative driver for MS SQL is available from Microsoft: ? http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx.

MSSQL 扩展在使用 PHP 5.3 或更高版本的 Windows 上不再可用。SQLSRV,Microsoft 提供的 MS SQL 替代驱动程序: ? http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

Once you downloaded that, follow the instructions at this page:

下载后,请按照此页面上的说明进行操作:

In a nutshell:

简而言之:

Put the driver file in your PHP extension directory.
Modify the php.ini file to include the driver. For example:

extension=php_sqlsrv_53_nts_vc9.dll  

Restart the Web server.

将驱动程序文件放在 PHP 扩展目录中。
修改 php.ini 文件以包含驱动程序。例如:

extension=php_sqlsrv_53_nts_vc9.dll  

重新启动 Web 服务器。

See Also (copied from that page)

另请参阅(从该页面复制)

The PHP Manual for the SQLSRV extension is located at http://php.net/manual/en/sqlsrv.installation.phpand offers the following for Installation:

SQLSRV 扩展的 PHP 手册位于http://php.net/manual/en/sqlsrv.installation.php并提供以下安装:

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 several driver files. Which driver file you use will depend on 3 factors: the PHP version you are using, whether you are using thread-safe or non-thread-safe PHP, and whether your PHP installation was compiled with the VC6 or VC9 compiler. For example, if you are running PHP 5.3, you are using non-thread-safe PHP, and your PHP installation was compiled with the VC9 compiler, you should use the php_sqlsrv_53_nts_vc9.dll file. (You should use a non-thread-safe version compiled with the VC9 compiler if you are using IIS as your web server). If you are running PHP 5.2, you are using thread-safe PHP, and your PHP installation was compiled with the VC6 compiler, you should use the php_sqlsrv_52_ts_vc6.dll file.

通过将适当的 DLL 文件添加到 PHP 扩展目录和 php.ini 文件的相应条目来启用 SQLSRV 扩展。SQLSRV 下载带有几个驱动程序文件。您使用哪个驱动程序文件将取决于 3 个因素:您使用的 PHP 版本、您使用的是线程安全的还是非线程安全的 PHP,以及您的 PHP 安装是使用 VC6 还是 VC9 编译器编译的。例如,如果您运行的是 PHP 5.3,您使用的是非线程安全的 PHP,并且您的 PHP 安装是使用 VC9 编译器编译的,那么您应该使用 php_sqlsrv_53_nts_vc9.dll 文件。(如果您使用 IIS 作为您的 Web 服务器,则应使用使用 VC9 编译器编译的非线程安全版本)。如果您运行的是 PHP 5.2,则您使用的是线程安全的 PHP,并且您的 PHP 安装是使用 VC6 编译器编译的,

The drivers can also be used with PDO.

这些驱动程序也可以与 PDO 一起使用

回答by danishgoel

Download Microsoft Drivers for PHP for SQL Server. Extract the files and use one of:

下载Microsoft Drivers for PHP for SQL Server。提取文件并使用以下之一:

File                             Thread Safe         VC Bulid
php_sqlsrv_53_nts_vc6.dll           No                  VC6
php_sqlsrv_53_nts_vc9.dll           No                  VC9
php_sqlsrv_53_ts_vc6.dll            Yes                 VC6
php_sqlsrv_53_ts_vc9.dll            Yes                 VC9

You can see the Thread Safety status in phpinfo().

您可以在 phpinfo() 中查看线程安全状态。

Add the correct file to your ext directory and the following line to your php.ini:

将正确的文件添加到您的 ext 目录,并将以下行添加到您的 php.ini:

extension=php_sqlsrv_53_*_vc*.dll

Use the filename of the file you used.

使用您使用的文件的文件名。

As Gordon already posted this is the new Extension from Microsoft and uses the sqlsrv_* API instead of mssql_*

正如 Gordon 已经发布的那样,这是 Microsoft 的新扩展,并使用 sqlsrv_* API 而不是 mssql_*

Update:
On Linux you do not have the requisite drivers and neither the SQLSERV Extension.
Look at Connect to MS SQL Server from PHP on Linux?for a discussion on this.

更新:
在 Linux 上,您没有必要的驱动程序,也没有 SQLSERV 扩展。
看看在Linux 上从 PHP 连接到 MS SQL Server?对此进行讨论。

In short you need to install FreeTDS and YES you need to use mssql_* functionson linux. see update 2

简而言之,您需要安装 FreeTDS ,是的,您需要在 linux 上使用 mssql_* 函数见更新 2

To simplify things in the long run I would recommend creating a wrapper class with requisite functions which use the appropriate API (sqlsrv_* or mssql_*) based on which extension is loaded.

从长远来看,为了简化事情,我建议创建一个带有必需函数的包装类,这些函数根据加载的扩展使用适当的 API(sqlsrv_* 或 mssql_*)。

Update 2:You do not need to use mssql_* functions on linux. You can connect to an ms sql server using PDO + ODBC + FreeTDS. On windows, the best performing method to connect is via PDO + ODBC + SQL Native Client since the PDO + SQLSRV driver can be incredibly slow.

更新 2:您不需要在 linux 上使用 mssql_* 函数。您可以使用 PDO + ODBC + FreeTDS 连接到 ms sql 服务器。在 Windows 上,性能最好的连接方法是通过 PDO + ODBC + SQL Native Client,因为 PDO + SQLSRV 驱动程序可能非常慢。