Home >Database >Mysql Tutorial >Why is `mysql_connect()` undefined in PHP 7 and how can I fix it?
PHP7 Fatal Error: mysql_connect() Undefined Function
When attempting to establish a connection between PHP and MySQL in XAMPP, an "Uncaught Error" may occur, indicating that the mysql_connect() function is undefined. This error often arises in PHP versions 7 onwards.
Issue Details
Specifically, the error manifests on line 22 of the code, where mysql_connect() is called to establish the connection.
$link = mysql_connect($mysql_hostname , $mysql_username);
Resolution
The issue stems from the deprecation of the MySQL extension in PHP 7. In its place, PHP7 provides two alternatives: MySQLi and PDO. Both offer similar functionalities to mysql_connect().
Alternative 1: MySQLi
Replace the mysql_connect() call with the following:
$link = mysqli_connect($mysql_hostname, $mysql_username, $mysql_password, $mysql_database);
Alternative 2: PDO
$link = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_database", $mysql_username, $mysql_password);
The above is the detailed content of Why is `mysql_connect()` undefined in PHP 7 and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!