当我try 从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来 suppress 弃用错误:

    error_reporting = E_ALL ^ E_DEPRECATED
    

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

推荐答案

  1. 为什么会发生这种事?

    整个ext/mysql PHP扩展(提供所有以前缀mysql_命名的函数)是officially deprecated in PHP v5.5.0removed in PHP v7.

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

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

  2. 我该怎么修?

    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 best waySQL injection attacks).PHP开发者Ulf Wendel写了a thorough comparison of the features篇文章.

    Hashphp.org有一个excellent tutorial on migrating from ext/mysql to PDO.

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

    error_reporting = E_ALL ^ E_DEPRECATED
    

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

    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.


你该怎么办?

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

    执行回归测试将是明智的:在您确定了所有可能的影响领域、围绕每个领域进行规划并在试运行环境中彻底测试您的解决方案之前,您确实不应该更改anything(特别是升级PHP).

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

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

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

      考虑一下你是否真的需要升级到PHP V5.5.此时此刻.

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

      但是,如果您有urgent的需要立即升级PHP,那么您可能暂时考虑 suppress 弃用错误:但是首先要确保识别出其他被丢弃的错误.

  • You are using a third party project that depends upon 100.

    考虑一下你是否真的需要升级到PHP V5.5.此时此刻.

    判断开发人员是否发布了与此特定问题相关的任何修复、解决方法或指导;如果没有,则通过提请他们注意此问题来迫使他们这样做.如果您有urgent%的需求需要立即升级PHP,那么您可以考虑暂时 suppress 弃用错误:但首先要确保识别出也在抛出的任何其他弃用错误.

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

Php相关问答推荐

将一个数字分配到一系列因素中(就像贪婪算法)

如何在数据库中添加注册表单中的动态新行?

使用.htaccess将任何路由重定向到文件

具有重叠捕获组的PHP正则表达式

WooCommerce/WordPress:简单的产品属性显示

从WooCommerce订单状态更改更新用户自定义元数据

PHP Laravel Web应用启动时间&>15秒

有没有可能从composer 过时的输出中隐藏不需要的主要版本?

如何按类别过滤自定义帖子类型

WooCommerce 以编程方式添加的费用不会持续存在

如何在 PHP laravel 中将图像的透明背景转换为白色?

使用 Spomky Labs JWT 框架中的 verifyWithKey 验证 id_token JWT 的签名需要 30 秒以上

使用 foreach php 将记录正确分配给正确的父级

使用 Composer 找不到 Google APIClient 类

在 WooCommerce 我的帐户自定义部分显示未购买的产品

Symfony:指定 data_class 时,提交的表单获取初始化前不得访问

同一页面上多个组件的 Livewire 分页问题

yii2 sql迁移覆盖新更改的记录

php preg_match 验证 Tiktok 用户名

Laravel Websockets 错误:(WebSocket 在建立连接之前关闭)