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

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

DDD
Release: 2024-12-11 03:45:18
Original
818 people have browsed it

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');
Copy after login
  • 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');
Copy after login
  • 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);
Copy after login
Copy after login
  • 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);
Copy after login
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template