Methods that we send queries such as query or exec return false if they encounter an error when they execute the query. We will use the errorInfo method to reach the error messages of these unsuccessful queries.

This method returns an array containing the code and the message of the error in our last query. In this series with 3 elements, the 0th and 1st element give error codes, and the 2nd element gives the error message.

<?php
if($users = $db->query('SELECT * FROM users WHERE'))
{
    // If the query runs successfully, we will list the members
}
else
{
    echo 'An error has occurred in the query.';
    $error = $db->errorInfo();
    echo 'Error message: ' . $error[2];
}
?>

After we say WHERE in the query in the example, since we do not specify any conditions, it will give an error and write the message of the related error on the screen.