PHP Error Handling: "Fatal error: Call to undefined function mysql_connect() [duplicate]"
Question:
When attempting to connect to a MySQL database using PHP, you encounter the error message "Fatal error: Call to undefined function mysql_connect()." Despite configuring PHP, MySQL, and Apache correctly, this issue persists.
Answer:
This error typically arises when you have recently upgraded your PHP version to PHP 7. In this version, the mysql_connect() function has been deprecated. To resolve this issue:
Verify your PHP version:
Use the command php -version to check your PHP version.
Switch to mysqli_connect():
Replace the deprecated mysql_connect() function with its corresponding mysqli_connect() function. For example:
$host = "127.0.0.1"; $username = "root"; $pass = "foobar"; $con = mysqli_connect($host, $username, $pass, "your_database");
Upgrade legacy PHP code:
If you are working with legacy PHP code, you must upgrade all instances of mysql_ functions to mysqli_ functions.
The above is the detailed content of Why Does PHP 7 Throw a \'Fatal error: Call to undefined function mysql_connect()\' Error?. For more information, please follow other related articles on the PHP Chinese website!