Home >Backend Development >PHP Tutorial >How to Fix the Deprecated Warning: `mysql_connect()`?

How to Fix the Deprecated Warning: `mysql_connect()`?

DDD
DDDOriginal
2024-12-11 03:45:18800browse

How to Fix the Deprecated Warning: `mysql_connect()`?

Deprecated Warning: mysql_connect()

Problem:

The MySQL code is triggering a deprecated warning, indicating that the mysql_connect() function is obsolete and will be removed in the future.

Solution:

To eliminate this warning, you have several options:

  • Use MySQLi: MySQLi is the improved and recommended extension for connecting to MySQL databases. You can use the following code:
$connection = mysqli_connect('localhost', 'username', 'password', 'database');
  • Use PDO: PDO (PHP Data Objects) is a more versatile and modern extension that supports multiple databases. You can use the following code:
$connection = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
  • Disable Deprecated Warnings: You can suppress the deprecated warnings by modifying the error_reporting setting in your PHP script. Add the following code:
error_reporting(E_ALL ^ E_DEPRECATED);
  • Update Your PHP Version: Upgrading to a newer version of PHP may automatically handle the deprecation of mysql_connect().

Specific File and Line Location for Error:

If you are receiving the exact error message "/System/Startup.php > line: 2 " error_reporting(E_All);", you can resolve it by replacing that line with the following:

error_reporting(E_ALL ^ E_DEPRECATED);

The above is the detailed content of How to Fix the Deprecated Warning: `mysql_connect()`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn