How can one effectively handle error messages in PHP MySQL queries?

When handling error messages in PHP MySQL queries, it is important to use error handling techniques to catch and display any potential errors that may occur during the execution of the query. One effective way to handle errors is by using the try-catch block to catch any exceptions that are thrown and then display the error message to the user.

try {
    // Your MySQL query code here
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}