I created my own code to execute MySQL statements and raise exceptions if they fail, including specific exceptions for various reasons for the failure. One of the most useful of these is a collision, which allows you to use try..catch and catch DatabaseCollisionException s blocks and handle them differently from other database exceptions.
What I found easiest for this was the MVC design pattern, where each table was represented by a class (models) of PHP, which I could simply assign as member variables and call the save method to save to the database, similar to
try { $user = new User(); $user->username = 'bob'; $user->setPassword($_POST['password'); // Could do some SHA1 functions or whatever $user->save } catch(DatabaseCollisionException $ex) { displayMessage("Sorry, that username is in use"); } catch(DatabaseException $ex) { displayMessage("Sorry, a database error occured: ".$ex->message()); } catch(Exception $ex) { displayMessage("Sorry, an error occured: ".$ex->message()); }
For more information on similar design patterns, see
Of course, this is not the only answer, these are just some ideas that may come in handy.
source share