pdo catch and output mysql errors [duplicate]

Solution 1:

By default PDO is not in a state that will display errors. you need to provide the following in your DB connection

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

More info can be seen Here

Solution 2:

1.Add ERRMODE_EXCEPTION mode after your db connection:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

2.And than you must use try{} catch{} method for all your mysql query. Like this:

try {
    $SQL = "DELETE FROM items WHERE item_id=:item_id";
    $m = $dbh->prepare($SQL);
    $m->bindParam(':item_id', $item_id, PDO::PARAM_INT);
    $m->execute();
    //success
    $return = "Your success message.";
}
catch (PDOException $e) {
    //error
    $return = "Your fail message: " . $e->getMessage();
}

Solution 3:

You should use this:

return $exception->getMessage();

See the page on the documentation of Exception class:

http://www.php.net/manual/en/exception.getmessage.php