Error Resolution: Understanding "Undefined Function mysql_connect() [duplicate]"
The "Undefined function mysql_connect()" error indicates that a PHP script is attempting to use the mysql_* functions, which are deprecated and removed in PHP7. Upon installing PHP5-mysql and restarting MySQL/Apache 2, you still encounter this error. This article delves into the solution to this issue.
PHP7 Removal of mysql_* Functions
In PHP7, the mysql_ functions have been completely removed. This includes the mysql_connect() function used to establish a connection to a MySQL database. Instead, you should utilize PDO-functions or mysqli_ functions.
Workaround for PHP7
If updating your code to PDO-functions or mysqli_* functions is not an option, consider the following workaround:
Create a PHP include file named "fix_mysql.inc.php" and include it in your PHP script that uses the mysql_ functions. This file recreates the old mysql_ functions using the mysqli_*()-functions.
// fix_mysql.inc.php <?php function mysql_connect($host, $username, $password) { return mysqli_connect($host, $username, $password); } function mysql_close($link) { return mysqli_close($link); } // ... Define other mysql_* functions as needed ... ?>
Example Usage
Include the "fix_mysql.inc.php" file in your PHP script:
<?php include 'fix_mysql.inc.php'; $link = mysql_connect('localhost', 'root', 'mypassword'); // Continue using the mysql_* functions as before ?>
Note: This workaround only recreates the basic functionality of the mysql_ functions. For advanced scenarios, it is recommended to update your code to PDO-functions or mysqli_ functions.
The above is the detailed content of Why Am I Still Getting \'Undefined Function mysql_connect()\' Error After Installing PHP5-mysql?. For more information, please follow other related articles on the PHP Chinese website!