Home >Database >Mysql Tutorial >How to Fix 'Deprecated: mysql_connect(): The mysql extension is deprecated' in PHP?
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
The syntax for connecting using MySQLi is:
$connection = mysqli_connect('localhost', 'username', 'password', 'database');
2. Use PDO (PHP Data Objects)
The PDO syntax for connecting to a MySQL database is:
$connection = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password');
3. Disable Deprecated Warnings
To do this, add the following line to your script:
error_reporting(E_ALL ^ E_DEPRECATED);
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!