php mysql 扩展已弃用,将来会被删除:改用 mysqli 或 PDO

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

The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

mysqldeprecatedphp

提问by eggyal

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

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

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 扩展已弃用,将来会被删除:在第 123 行的 /path/to/filename.php 中使用 mysqli 或 PDO 代替

The code on the referenced line is:

引用行上的代码是:

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

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.

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

  1. Why is this happening?

  2. How can I fix it?

  3. I understand that it's possible to suppress deprecation errors by setting error_reportingin php.inito exclude E_DEPRECATED:

    error_reporting = E_ALL ^ E_DEPRECATED
    

    What will happen if I do that?

  1. 为什么会这样?

  2. 我该如何解决?

  3. 据我所知,它通过设置是可以抑制弃用错误error_reportingphp.ini排除E_DEPRECATED

    error_reporting = E_ALL ^ E_DEPRECATED
    

    如果我这样做会发生什么?

回答by eggyal

  1. Why is this happening?

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

    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.

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

  2. How can I fix it?

    As the error message suggests, there are two other MySQL extensions that you can consider: MySQLiand 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.

    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 wayto defeat SQL injection attacks). PHP developer Ulf Wendel has written a thorough comparison of the features.

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

  3. I understand that it's possible to suppress deprecation errors by setting error_reportingin php.inito exclude E_DEPRECATED:

    error_reporting = E_ALL ^ E_DEPRECATED
    

    What will happen if I do that?

    Yes, it is possible to suppress such error messages and continue using the old ext/mysqlextension 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.

    Note also that this technique will suppress allE_DEPRECATEDmessages, not just those to do with the ext/mysqlextension: 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 allerrors raised by that expression, not just E_DEPRECATEDones.

  1. 为什么会这样?

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

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

    该手册包含了自 2011 年 6 月以来在新代码中使用它的警告。

  2. 我该如何解决?

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

    他们略有不同,但提供了一些优于旧的扩展包括交易API的支持,存储过程和预处理语句(从而提供最佳的方式击败SQL注入攻击)。PHP 开发人员 Ulf Wendel 已编写了对这些功能的全面比较

    Hashphp.org 有一个关于从ext/mysqlPDO迁移优秀教程

  3. 据我所知,它通过设置是可以抑制弃用错误error_reportingphp.ini排除E_DEPRECATED

    error_reporting = E_ALL ^ E_DEPRECATED
    

    如果我这样做会发生什么?

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

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



What should you do?

你该怎么办?

  • You are starting a new project.

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

  • You have (your own) legacy codebase that currently depends upon ext/mysql.

    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.

    • Following good coding practice, your application was developed in a loosely integrated/modular fashion and the database access methods are all self-contained in one place that can easily be swapped out for one of the new extensions.

      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.

    • The database access methods are scattered all over the place and cannot easily be swapped out for one of the new extensions.

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

      You should begin planning to replace ext/mysqlwith 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.

      However, if you have an urgentneed 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.

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

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

    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 urgentneed 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.

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

    这里是绝对没有理由使用ext/mysql其他的,更现代化,扩展,而不是一个-选择并获得他们所提供的好处的回报。

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

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

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

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

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

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

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

      但是,如果您迫切需要立即升级 PHP,您可能会考虑暂时抑制弃用错误:但首先要确保识别出任何其他也被抛出的弃用错误。

  • 您正在使用依赖于ext/mysql.

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

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

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