Lovelock
Lovelock

Reputation: 8085

PDO not throwing any errors

I have gone back to some basic PDO after using Laravel for a few months and hit issues already.

Example is this code:

try {
    $conn = new PDO('mysql:host=127.0.0.1;dbname=every_hour', 'root', 'root');

    $query = "INSERT INTO votes (image_id, created_at) VALUES (:image_id, '22'";
    $q = $conn->prepare($query);
    $q->execute(
      [
        ':image_id' => $image_id
      ]);

      $return['success'] = true;

    } catch(PDOException $e) {
      echo json_encode($e->getMessage());
    }

As you can see the last bracket is missing from the query.

But this isn't throwing any errors... whats missing?

Upvotes: 2

Views: 58

Answers (1)

vhu
vhu

Reputation: 12798

You need to enable Exceptions. Use setAttribute() after you create the PDO object.

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

See manual for details.

Upvotes: 3

Related Questions