1

I have this try and catch:

try {
    $stmt = $this->pdo->prepare("Select ...");
    $stmt->execute() or die(Log::error("DB-Error (Reservation): ", ['error' => $stmt->errorInfo()]));
    return $stmt->fetchAll(PDO::FETCH_OBJ);
} catch (PDOException $e) {
    Log::error("DB-Error (Reservation): ", ['error' => $e->getMessage(), 'errorcode' => (int)$e->getCode()]);
    return false;
} catch (\Exception $e) {
    Log::error("Error (Reservation): ", ['error' => $e->getMessage()]);
    return false;
}

I have 1 questions:

  1. Is this one needed: $stmt->execute() or die(Log::error("DB-Error (Reservation): ", ['error' => $stmt->errorInfo()])); or can I simply use $stmt->execute() or die(); because I catch the error later?
Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
  • 4
    There should be no `die` at all. _If_ you let the script die at this point, then there will be no catching of anything _later_ - dead is dead. – 04FS Sep 25 '20 at 10:08
  • Not sure if it is a dupe of https://stackoverflow.com/questions/15318368/mysqli-or-die-does-it-have-to-die (although that is myqli not pdo - but the principle is the same). – Nigel Ren Sep 25 '20 at 10:18

1 Answers1

1

There is should be no die you can try code below:

try {
    $stmt = $this->pdo->prepare("Select ...");
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_OBJ);
} catch (PDOException $e) {
    Log::error("DB-Error (Reservation): ", ['error' => $e->getMessage(), 'errorcode' => (int)$e->getCode()]);
    return new stdClass();
} catch (\Exception $e) {
    Log::error("Error (Reservation): ", ['error' => $e->getMessage()]);
    return new stdClass();
}
Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
  • When I use this in PHPStorm, I get this notice `Undefined class 'stdClass'` –  Sep 25 '20 at 10:31