mysql 扩展已弃用,将来会被删除:改用 mysqli 或 PDO [英] The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

查看:49
本文介绍了mysql 扩展已弃用,将来会被删除:改用 mysqli 或 PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从 PHP 连接到 MySQL 服务器时,我看到以下错误:

When I attempt to connect to a MySQL server from PHP, I see the following error:

已弃用:mysql 扩展已弃用,将来会被删除:在第 123 行的/path/to/filename.php 中使用 mysqli 或 PDO 代替

Deprecated: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /path/to/filename.php on line 123

引用行上的代码是:

mysql_connect($server, $username, $password);

我确信这些论点是正确的,这个确切的代码多年来一直没有问题.事实上,我是从一个关于 PHP 的资源丰富的教程中获得的.

I am certain that the arguments are correct, and this exact code has been working for years without problem. Indeed, I obtained it from a well-sourced tutorial on PHP.

  1. 为什么会这样?

  1. Why is this happening?

我该如何解决?

我知道可以通过在 php.ini 中设置 error_reporting 以排除 E_DEPRECATED 来抑制弃用错误:

I understand that it's possible to suppress deprecation errors by setting error_reporting in php.ini to exclude E_DEPRECATED:

error_reporting = E_ALL ^ E_DEPRECATED

如果我这样做会怎样?

推荐答案

为什么会这样?

整个 ext/mysql PHP 扩展,它提供所有以前缀 mysql_ 命名的函数,是 在 PHP v5.5.0 中正式弃用已在 PHP v7 中移除.

The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, was officially deprecated in PHP v5.5.0 and removed in PHP v7.

它最初是在 MySQL v3.20 的 PHP v2.0(1997 年 11 月)中引入的,自 2006 年以来没有添加任何新功能.加上缺乏新功能,在复杂的安全漏洞中很难维护这样的旧代码.

It was originally introduced in PHP v2.0 (November 1997) for MySQL v3.20, and no new features have been added since 2006. Coupled with the lack of new features are difficulties in maintaining such old code amidst complex security vulnerabilities.

自 2011 年 6 月以来,该手册已包含针对其在新代码中使用的警告.

The manual has contained warnings against its use in new code since June 2011.

我该如何解决?

如错误消息所示,您可以考虑另外两个 MySQL 扩展:MySQLiPDO_MySQL,其中任何一个都可以代替ext/mysql.自 v5.0 以来,两者都已在 PHP 核心中使用,因此如果您使用的版本会引发这些弃用错误,那么您几乎可以肯定地立即开始使用它们——即无需任何安装工作.

As the error message suggests, there are two other MySQL extensions that you can consider: MySQLi and PDO_MySQL, either of which can be used instead of ext/mysql. Both have been in PHP core since v5.0, so if you're using a version that is throwing these deprecation errors then you can almost certainly just start using them right away—i.e. without any installation effort.

它们略有不同,但与旧扩展相比提供了许多优势,包括对事务、存储过程和准备好的语句的 API 支持(从而提供了击败SQL注入攻击的最佳方法).PHP 开发人员 Ulf Wendel 编写了 对功能的全面比较.

They differ slightly, but offer a number of advantages over the old extension including API support for transactions, stored procedures and prepared statements (thereby providing the best way to defeat SQL injection attacks). PHP developer Ulf Wendel has written a thorough comparison of the features.

Hashphp.org 有一个 关于从 ext/mysql 迁移到 PDO 的优秀教程.

Hashphp.org has an excellent tutorial on migrating from ext/mysql to PDO.

我知道可以通过在 php.ini 中设置 error_reporting 来排除 E_DEPRECATED 来抑制弃用错误:

I understand that it's possible to suppress deprecation errors by setting error_reporting in php.ini to exclude E_DEPRECATED:

error_reporting = E_ALL ^ E_DEPRECATED

如果我这样做会怎样?

是的,可以抑制此类错误消息并暂时继续使用旧的 ext/mysql 扩展.但是您真的不应该这样做—这是开发人员的最后警告,即该扩展可能不会与 PHP 的未来版本捆绑在一起(实际上,正如已经提到的,它已从 PHP 中删除v7).相反,您应该利用这个机会现在迁移您的应用程序,以免为时已晚.

Yes, it is possible to suppress such error messages and continue using the old ext/mysql extension for the time being. But you really shouldn't do this—this is a final warning from the developers that the extension may not be bundled with future versions of PHP (indeed, as already mentioned, it has been removed from PHP v7). Instead, you should take this opportunity to migrate your application now, before it's too late.

另请注意,此技术将抑制所有 E_DEPRECATED 消息,而不仅仅是与 ext/mysql 扩展有关的消息:因此您可能不知道会影响您的应用程序代码的其他 PHP 即将发生的更改.当然,可以使用 PHP 的 错误控制运算符—ie在相关行前面加上 @—然而,这将抑制该表达式引发的所有错误,而不仅仅是 E_DEPRECATED 错误.

Note also that this technique will suppress all E_DEPRECATED messages, not just those to do with the ext/mysql extension: therefore you may be unaware of other upcoming changes to PHP that would affect your application code. It is, of course, possible to only suppress errors that arise on the expression at issue by using PHP's error control operator—i.e. prepending the relevant line with @—however this will suppress all errors raised by that expression, not just E_DEPRECATED ones.

<小时>

你应该怎么做?

  • 您正在开始一个新项目.

    绝对没有理由使用 ext/mysql—选择其他更现代的扩展之一,并从它们提供的好处中获益.

    There is absolutely no reason to use ext/mysql—choose one of the other, more modern, extensions instead and reap the rewards of the benefits they offer.

    您有(您自己的)遗留代码库,目前依赖于 ext/mysql.

    执行回归测试是明智之举:您真的不应该改变任何事情(尤其是升级 PHP),直到您确定了所有潜在的影响领域,并围绕每个领域进行了规划和然后在临时环境中彻底测试您的解决方案.

    It would be wise to perform regression testing: you really shouldn't be changing anything (especially upgrading PHP) until you have identified all of the potential areas of impact, planned around each of them and then thoroughly tested your solution in a staging environment.

    • 遵循良好的编码习惯,您的应用程序是以松散集成/模块化的方式开发的,并且数据库访问方法都是自包含在一个地方,可以轻松地替换为新扩展之一.

    花半个小时重写这个模块以使用其他更现代的扩展;彻底测试.您可以稍后引入进一步的改进,以获得他们提供的好处的回报.

    Spend half an hour rewriting this module to use one of the other, more modern, extensions; test thoroughly. You can later introduce further refinements to reap the rewards of the benefits they offer.

    数据库访问方法分散在各处,不能轻易更换为新扩展之一.

    考虑此时是否真的需要升级到 PHP v5.5.

    Consider whether you really need to upgrade to PHP v5.5 at this time.

    您应该开始计划将 ext/mysql 替换为其他更现代的扩展之一,以便您可以从它们提供的好处中获益;您也可以借此机会将数据库访问方法重构为更加模块化的结构.

    You should begin planning to replace ext/mysql with one of the other, more modern, extensions in order that you can reap the rewards of the benefits they offer; you might also use it as an opportunity to refactor your database access methods into a more modular structure.

    但是,如果您紧急需要立即升级 PHP,您可以考虑暂时取消弃用错误:但首先要确保识别出任何其他也被抛出的弃用错误.

    However, if you have an urgent need to upgrade PHP right away, you might consider suppressing deprecation errors for the time being: but first be sure to identify any other deprecation errors that are also being thrown.

    您正在使用依赖于 ext/mysql 的第三方项目.

    You are using a third party project that depends upon ext/mysql.

    考虑此时是否真的需要升级到 PHP v5.5.

    Consider whether you really need to upgrade to PHP v5.5 at this time.

    检查开发者是否发布了与此特定问题相关的任何修复程序、解决方法或指南;或者,如果没有,通过提请他们注意此事来迫使他们这样做.如果您紧急需要立即升级 PHP,您可以考虑暂时取消弃用错误:但首先要确保识别出任何其他也被抛出的弃用错误.

    Check whether the developer has released any fixes, workarounds or guidance in relation to this specific issue; or, if not, pressure them to do so by bringing this matter to their attention. If you have an urgent need to upgrade PHP right away, you might consider suppressing deprecation errors for the time being: but first be sure to identify any other deprecation errors that are also being thrown.

    执行回归测试是绝对必要的.

    It is absolutely essential to perform regression testing.

    这篇关于mysql 扩展已弃用,将来会被删除:改用 mysqli 或 PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆