Home » Php » Why doesn't my code knows there is something wrong PHP PDO

Why doesn't my code knows there is something wrong PHP PDO

Posted by: admin February 25, 2020 Leave a comment

Questions:

This is my try statement

$query = "DELETEh FROM supers WHERE id = :id";
    try {
      $stmt = $conn->prepare($query);
      $stmt->bindValue(':id', $id);
      $stmt->execute();
      $_SESSION['notification'] = "Hero was deleted sucessfully";
    }

and this is the rest of the code

 catch(PDOException $e) {
$_SESSION['notification'] = "There was an error deleting hero: " . $e->getMessage();


 }
  header("Location: notification.php");
  exit;

when this runs, it tells me that the hero was deleted successfully, even though it did not because there is an extra “h” after DELETE. Could anyone explain why is this not getting into the catch block please?

How to&Answers:

Most likely you didn’t do

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

when you opened the PDO connection. The default is PDO::ERRMODE_SILENT. $conn->prepare() will return FALSE and you need to get the reason from $conn->errorCode() or $conn->errorInfo().