How can PHP developers effectively handle errors and exceptions when executing MySQL queries?

When executing MySQL queries in PHP, developers can effectively handle errors and exceptions by using try-catch blocks to catch any potential exceptions thrown by the database operations. By wrapping the query execution code within a try block and using catch blocks to handle specific exceptions, developers can gracefully manage errors and provide appropriate feedback to users.

try {
    // Connect to MySQL database
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Execute MySQL query
    $result = $conn->query("SELECT * FROM table");

    // Process query results
    // ...

} catch (Exception $e) {
    echo "Error executing MySQL query: " . $e->getMessage();
}