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

查看:380
本文介绍了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

如果我这样做会怎样?

推荐答案

为什么会这样?

Why is this happening?

整个ext/mysql PHP扩展(提供所有以前缀mysql_命名的功能)为在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.

我该如何解决?

How can I fix it?

如错误消息所示,您可以考虑另外两个MySQL扩展: MySQLi PDO_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支持,存储过程和准备好的语句(从而提供 the最好的方法来战胜 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

如果我那样做将会怎样?

What will happen if I do that?

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

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.

还请注意,此技术将抑制 all E_DEPRECATED条消息,而不仅仅是与ext/mysql扩展名相关的消息:因此,您可能没有意识到即将对PHP进行的其他更改,这些更改可能会影响您的应用程序代码.当然,通过使用PHP的

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.

      完全没有理由使用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.

      You have (your own) legacy codebase that currently depends upon 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天全站免登陆