How can the mysql_error() function be used to troubleshoot PHP MySQL database connection issues?

When troubleshooting PHP MySQL database connection issues, the mysql_error() function can be used to display any error messages generated by MySQL. This can help identify the specific problem with the database connection, such as incorrect credentials or server issues. By capturing and displaying these error messages, developers can quickly diagnose and address the root cause of the connection problem.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>