How can the PHP code be modified to provide a more accurate error message when a connection cannot be established?

When a connection cannot be established in PHP, it is important to provide a more accurate error message to help with troubleshooting. One way to achieve this is by using the `mysqli_connect_error()` function to retrieve the specific error message when a connection fails. This message can then be displayed to the user or logged for further analysis.

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

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

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>