不推荐使用 mysql 扩展并将在将来删除:请改用 mysqli 或 PDO [英] The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

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

问题描述

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

<块引用>

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

引用行上的代码是:

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

我确信这些论点是正确的,而且这个确切的代码已经运行多年没有问题.事实上,我是从一个资源丰富的 PHP 教程中获得它的.

  1. 为什么会这样?

  2. 我该如何解决?

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

    error_reporting = E_ALL ^ E_DEPRECATED

    如果我这样做会怎样?

解决方案

  1. <块引用>

    为什么会这样?

    整个 ext/mysql PHP 扩展,它提供了所有以 mysql_ 为前缀的函数,是 在 PHP v5.5.0在 PHP v7 中删除.

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

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

  2. <块引用>

    我该如何解决?

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

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

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

  3. <块引用>

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

    error_reporting = E_ALL ^ E_DEPRECATED

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

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

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

<小时>

你应该怎么做?

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

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

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

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

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

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

    • 数据库访问方法分散在各处,不能轻易换成新的扩展.

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

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

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

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

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

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

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

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

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

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.

  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_reporting in php.ini to exclude E_DEPRECATED:

    error_reporting = E_ALL ^ E_DEPRECATED
    

    What will happen if I do that?

解决方案

  1. Why is this happening?

    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.

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

    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 has an excellent tutorial on migrating from ext/mysql to PDO.

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

    What will happen if I do that?

    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.

    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.


What should you do?

  • You are starting a new project.

    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.

  • 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/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.

      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.

  • 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 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天全站免登陆