Home > Database > Mysql Tutorial > How to Fix 'Deprecated: mysql_connect(): The mysql extension is deprecated' in PHP?

How to Fix 'Deprecated: mysql_connect(): The mysql extension is deprecated' in PHP?

DDD
Release: 2024-12-10 16:19:10
Original
499 people have browsed it

How to Fix

Deprecated: mysql_connect() Warning in PHP

When attempting to establish a database connection using the mysql_connect() function, users may encounter the warning: "Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead." This deprecation message is a notification that the mysql extension in PHP is becoming obsolete and will eventually be removed.

To resolve this issue and eliminate the warning message, there are two main approaches:

1. Migrate to the MySQLi Extension

  • Migrate to the MySQLi extension, which provides an improved and modern interface for interacting with MySQL databases.
  • The syntax for connecting using MySQLi is:

    $connection = mysqli_connect('localhost', 'username', 'password', 'database');
    Copy after login
  • Queries can be executed using the mysqli_query() function instead of the older mysql_query() function.

2. Use PDO (PHP Data Objects)

  • Use PDO (PHP Data Objects), which offers a unified interface for accessing various database systems, including MySQL.
  • The PDO syntax for connecting to a MySQL database is:

    $connection = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password');
    Copy after login
  • Queries can be executed using the PDO::query() method.

3. Disable Deprecated Warnings

  • If you are unable to migrate to MySQLi or PDO immediately, you can disable all deprecated warnings, including those from the mysql_* functions.
  • To do this, add the following line to your script:

    error_reporting(E_ALL ^ E_DEPRECATED);
    Copy after login

Note that disabling deprecated warnings is not a long-term solution and it is recommended to migrate to a supported extension such as MySQLi or PDO eventually.

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

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