How can one handle database errors in PHP when using MySQLi?

When handling database errors in PHP with MySQLi, you can use the try-catch block to catch any exceptions thrown by the database operations. Within the catch block, you can retrieve the error message and code to handle the error accordingly.

try {
    // Perform database operations using MySQLi
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
    // Handle the error appropriately, such as logging or displaying a user-friendly message
}