I have the following scenario:
In my .db.inc.php file I'm connecting to a database:
try {
$dbh = new PDO('mysql:host=' . HOST . ';dbname=' . DATABASE , USER_DB, PASSWORD_DB);
}
catch (PDOException $e) {
die('Error!: ' . $e->getMessage() . '<br/>');
}
Then I run some functions before I need the connection, but I know that the connection is established as I don't have an error thrown.
Now I need to update my database:
include('.db.inc.php');
try {
/...
$sql = "UPDATE de_request_products SET report_id = :report_id WHERE request_id = :request_id";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(':report_id', $report_number, PDO::PARAM_STR);
$stmt->bindValue(':request_id', $report_request_number, PDO::PARAM_STR);
$stmt->execute();
}
catch (PDOException $e){
die('Error!: ' . $e->getMessage() . '<br/>');
}
If above written try block fails, I want to have a try catch block in the Exception Handler to update the database:
// ....
catch (PDOException $e){
try {
//SEND EMAIL AND
$sql = "INSERT INTO de_failed_requests (datum, reason) VALUES (:datum, :reason)";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(':datum', $date, PDO::PARAM_STR);
$stmt->bindValue(':reason', $e->getMessage(), PDO::PARAM_STR);
$stmt->execute();
} catch (PDOException $f) {
die('NOTHING WORKS AT ALL!'. $f->getMessage());
}
die('Error!: ' . $e->getMessage() . '<br/>');
}
Would this be the way to go or is there a more efficient way?