Why PHP is Displaying "Fatal error: Uncaught Error: Call to undefined function mysql_connect()"?
When attempting to interact with a MySQL database using XAMPP, it's possible to encounter the error "Fatal error: Uncaught Error: Call to undefined function mysql_connect()." This error indicates that the mysql_connect() function is not recognized by PHP, causing the connection to fail.
Understanding the Problem
The mysql_* functions, including mysql_connect(), were deprecated in PHP 5.5 and removed in PHP 7. This change was made due to concerns about security and performance. If you're still using PHP 7 or later, you won't be able to use mysql_connect() anymore.
Solution
To resolve this error, you have two alternatives:
Example Using MySQLi:
$mysqli = new mysqli($mysql_hostname, $mysql_username, $mysql_password); if ($mysqli->connect_error) { echo "Connection failed: " . $mysqli->connect_error; }
Example Using PDO:
$pdo = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_database", $mysql_username, $mysql_password);
Conclusion
Remember to use MySQLi or PDO when working with MySQL databases in PHP 7 or later. By adopting these modern alternatives, you'll not only resolve the "Fatal error: Uncaught Error: Call to undefined function mysql_connect()" issue but also improve the security and performance of your database interactions.
The above is the detailed content of Why is PHP Throwing a 'Fatal error: Uncaught Error: Call to undefined function mysql_connect()' and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!