I use try catch statements in almost every one of my insert or update statements on a site built with PHP and MySQL. In the event of something like:
try {
//do insert or update
}
catch (PDOException $e) {
$db->rollback();
//do something with error $e
}
in this example, if an error occurs, anything that was done to the DB will be rolled back (changes reversed) and all is “well”. I am not certain however how to handle situations in which multiple insert or update statements exist in one try/catch – how even multiple try/catches on one page.
Ultimately I am trying to solve: if an error is found in a script which does not allow a single insert/update (try/catch) statement to complete, to rollback the changes made to ALL tables within that particular script.
Is there a reliable way to do this?
You dont have to only have one action in each transaction. you can have multiple.
For example:
When you go to the bank’s atm: do you log in, get cash. log out. log back in. make a deposit. log out. (and on..)
OR Do you Log in. Get cash, make deposit. (and on…) log out. ?