Error: "Undefined Function mysql_connect()" in PHP
Introduction:
When attempting to establish a database connection using the mysql_connect() function, you may encounter the error "Call to undefined function mysql_connect()". This error indicates that the function is not recognized by PHP.
Issue:
The error typically arises when PHP is missing the necessary MySQL extension. Despite installing php5-mysql, the issue persists.
Resolution:
For PHP7 and Above:
In PHP7, the mysql_* functions have been removed due to deprecation. To resolve the error, you should update your code to use either PDO or MySQLi functions instead.
For PHP5:
Workaround:
If it's not feasible to update your code, you can utilize a workaround by creating an include file, fix_mysql.inc.php. This file recreates the old mysql_ functions with the mysqli_ functions. By including this file before using mysql_ functions, it will translate them to their mysqli_ equivalents. Here's an example of the workaround:
<?php // Include the fix_mysql.inc.php file include 'fix_mysql.inc.php'; // Use the mysql_* functions as usual $conn = mysql_connect('hostname', 'username', 'password'); ?>
Please note that this workaround is a temporary measure and it's still recommended to migrate your code to use PDO or MySQLi functions in the long run.
The above is the detailed content of Why Doesn't mysql_connect() Work in My PHP Code?. For more information, please follow other related articles on the PHP Chinese website!