Home >Backend Development >PHP Tutorial >How Can I Fix the 'Deprecated: mysql_connect()' Warning in My PHP Code?
Deprecated: mysql_connect() Warning in PHP
You may encounter the "Deprecated: mysql_connect()" warning while using MySQL functions in your PHP code. This warning indicates that the mysql_* extensions are deprecated and will be removed in future PHP versions.
Cause:
This warning is triggered because your code is using deprecated MySQL functions like mysql_connect(), which have been replaced with more modern and secure alternatives.
Solution:
There are two main approaches to resolving this warning:
Upgrade to MySQLi or PDO:
Suppress Deprecated Warnings:
Use the error_reporting() function to suppress deprecated warnings specifically for the mysql_* functions. For instance, add the following line at the top of your script:
error_reporting(E_ALL ^ E_DEPRECATED);
Example with MySQLi:
<?php $connection = mysqli_connect('localhost', 'username', 'password', 'database'); ?>
Additional Notes:
The above is the detailed content of How Can I Fix the 'Deprecated: mysql_connect()' Warning in My PHP Code?. For more information, please follow other related articles on the PHP Chinese website!