Errors are inevitable. They assail all of us and can, at times, be caused by circumstances outside our control—database crashes, database upgrades, downtime for maintenance, and so on. If something goes wrong when you’re trying to deal with PHP and SQL together, it’s often difficult to find the cause. The trick is to get PHP to tell you where the problem is, bearing in mind that you must be able to hide this information from visitors when the site goes live.
Quote:$country = 'USA';The default error mode sets the errorCode property of the PDOStatement object, but does nothing else. As you can see in this example, you need to check the error code manually to ascertain whether or not an error was found—otherwise your script will happily continue on its merry way.
$dbh = new PDO($dsn, $user, $password);
$sql = 'Select * from cities where CountryCode =:country';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':country', $country, PDO:ARAM_STR);
$stmt->execute();
$code = $stmt->errorCode();
if (empty($code))
{
⋮
proceed to fetch data
}
else
{
echo 'Error with the database: <br />';
echo 'SQL Query: ', $sql;
echo '<pre>';
var_dump($stmt->errorInfo());
echo '</pre>';
}