What are some best practices for error handling in PHP scripts, especially when dealing with MySQL connections?

When dealing with MySQL connections in PHP scripts, it is important to implement proper error handling to gracefully handle any potential issues that may arise. One best practice is to use try-catch blocks to catch exceptions that may be thrown when connecting to the database or executing queries. Additionally, utilizing functions like mysqli_connect_errno() and mysqli_connect_error() can help identify specific errors that occur during the connection process.

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

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Perform database operations here

// Close connection
$conn->close();
?>