What are the best practices for displaying MySQL errors in PHP when executing queries?

When executing MySQL queries in PHP, it is important to properly handle and display any errors that may occur to aid in debugging and troubleshooting. One common practice is to use the `mysqli_error()` function to retrieve the error message from MySQL and display it to the user or log it for further analysis.

// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Execute query
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Check for errors
if (!$result) {
    echo "Error: " . mysqli_error($connection);
}

// Close connection
mysqli_close($connection);