Encountering "Fatal error: Uncaught Error: Call to undefined function mysql_connect()" while attempting to interact with a database using XAMPP and MySQL server can be frustrating. Here's an explanation of the error and how to resolve it:
Understanding the Error
The error "Call to undefined function mysql_connect()" signifies that the mysql_connect() function, used to establish a connection to MySQL databases, is not recognized by PHP. This error occurs because the mysql_* functions, including mysql_connect(), have been deprecated in PHP 7 and removed entirely in PHP 8.
Resolution
With PHP 7 or later, alternative PHP extensions are required to interact with MySQL databases:
1. MySQLi Extension:
Use mysqli_connect() to establish a connection:
$link = mysqli_connect($mysql_hostname, $mysql_username);
2. PDO Extension:
Use new PDO() to establish a connection:
$link = new PDO('mysql:host=' . $mysql_hostname . ';dbname=' . $mysql_database, $mysql_username, $mysql_password);
Additional Notes:
The above is the detailed content of Why am I getting the \'Call to undefined function mysql_connect()\' error in PHP when connecting to a MySQL database?. For more information, please follow other related articles on the PHP Chinese website!