Home > Backend Development > PHP Tutorial > MySQL `mysql_connect()` Deprecated: Migrate to MySQLi or PDO?

MySQL `mysql_connect()` Deprecated: Migrate to MySQLi or PDO?

Mary-Kate Olsen
Release: 2024-12-10 17:10:15
Original
787 people have browsed it

MySQL `mysql_connect()` Deprecated: Migrate to MySQLi or PDO?

Deprecation of mysql_connect(): Migration to MySQLi or PDO

You have encountered the deprecation warning for mysql_connect(). This means that the mysql extension is being phased out and will eventually be removed. To eliminate this warning and ensure continued functionality, you need to migrate your code to either the MySQLi or PDO extensions.

Solution: Using MySQLi Extension

One option is to use the MySQLi extension, which is a modern replacement for the mysql extension. The syntax is slightly different, but it offers similar functionality. Below is an example:

<?php
$connection = mysqli_connect('localhost', 'username', 'password', 'database');
?>
Copy after login

Query Execution with MySQLi

Query execution with MySQLi is also similar to the mysql extension:

<?php
mysqli_query($connection, 'CREATE TEMPORARY TABLE `table`');
?>
Copy after login

Suppressing Deprecated Warnings

Alternatively, you can suppress all deprecated warnings, including those from mysql_* functions, by using the following code:

<?php
error_reporting(E_ALL ^ E_DEPRECATED);
?>
Copy after login

In some cases, you may need to locate the specific file and line where the warning is triggered. Once identified, you can replace error_reporting(E_All); with error_reporting(E_ALL ^ E_DEPRECATED);.

The above is the detailed content of MySQL `mysql_connect()` Deprecated: Migrate to MySQLi or PDO?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template