Jens Törnell
Jens Törnell

Reputation: 24748

PHP PDO - What needs to try catch?

I'm using PHP and PDO. Now I want to build a kind of log when things go wrong. What can go wrong with PDO?

Right now I have these tests:

Connection test

try {
    $this->pdo = new PDO($dsn, $credentials['user'], $credentials['pass'], $options);
} catch(Exception $e) {
    $this->file->put( date('Y-m-d') . '.txt', 'log', 'Database error');
}

Execute test

try {
    $stmt->execute();
} catch(Exception $e) {
    $this->error->log('SQL', 'query error');
}

Any more tests that are good?

Upvotes: 0

Views: 127

Answers (1)

Okipa
Okipa

Reputation: 561

You do not log your exception message in your logs. I suggest you to do something like this into your catch :

$this->error->log('SQL', $e . PHP_EOL);

This will give you more understandable and readable logs.

Regarding the exceptions to catch with PDO, you may read that post : How to handle PDO exceptions

Upvotes: 2

Related Questions