franko75 Posted December 5, 2008 Share Posted December 5, 2008 Hi, I'm just looking for a bit of advice about how best to handle database errors - mainly, when to use exceptions and when to use error messages/codes. If i have a database class which i use to perform a query/update/insert/delete - how would you handle any errors by the queries failing? for example, i could do this: public function query($sql) { if ($sql) { return mysql_query($sql); } else { $this->error = mysql_error(); return FALSE; } } and call it with: $db = new Database; $query = $db->query("SELECT id, title, content, item_date, date_posted FROM news where status = 1"); Is it good practice then to check for errors for every query you perform, like this: if ( ! $query) $error = $db->error; //stop processing and show message to user else //continue processing I''m interested in finding out how other people handle this. Previously I used procedural php and didn't really check properly for errors in these types of situation (not good practice i know!). Quote Link to comment https://forums.phpfreaks.com/topic/135660-advice-on-database-exceptionserrors/ Share on other sites More sharing options...
premiso Posted December 5, 2008 Share Posted December 5, 2008 I use my DB class to handle the errors. In production I check mysql_error after every query, if there is an error I send an email to myself with the error and where it occured etc. Then display a nice page to the user "We are sorry an error was encountered, the admin has been notified." If I am developing, I have it simply print the error to the page public function query($sql) { if ($sql) { $resource = mysql_query($sql); if (mysql_error()) { return $this->error(); }else { return $resource; } }else { $this->error = mysql_error(); return FALSE; } } Sorry bout the formating, copying from the [ php ] tags tends to do that. A side note I was taught when I first started programming, handle the errors where they are thrown. So instead of handling them somewhere else in the code, handle them at the source and save yourself alot of extra coding. Quote Link to comment https://forums.phpfreaks.com/topic/135660-advice-on-database-exceptionserrors/#findComment-706782 Share on other sites More sharing options...
franko75 Posted December 5, 2008 Author Share Posted December 5, 2008 @premiso: thanks for the advice, i'll definitely do something similar for the development and in production stages. In terms of catching errors where they are thrown, for system errors (e.g db connection issues) I am throwing exceptions, but for errors caused in sql queries etc, i am returning error messages with mysql_error() - does this seem a reasonable approach? Also just realised my code wasn't correct in the first post - I've updated it using your snippet as follows: public function query($sql) { if ($sql) { $resource = mysql_query($sql); if (mysql_error()) { $this->error = mysql_error(); return FALSE; } else { return $resource; } } else { $this->error = "No query supplied"; return FALSE; } } Quote Link to comment https://forums.phpfreaks.com/topic/135660-advice-on-database-exceptionserrors/#findComment-706810 Share on other sites More sharing options...
premiso Posted December 5, 2008 Share Posted December 5, 2008 imo, I have a function in the class called error(). When this is called it sends an email with the error information to me, if that is setup and then redirects the user to the page (session helps me keep track of pages the user was at), so nothing is returned. If I am doing it in development, I simply print the error to the screen, then die the script. Cause if you get that error chances are the rest of the script is prone to errors etc due to that one single error, so why run it if you know the next errors to post cede it are potentially false errors? At least that is my philosphy. Anytime I am using classes and I get an error, I always send it to the error function and depending what it is kill the script or do something else. Th email sent ensures that you know there is a problem, where it is and allows you to fix it without the user every seeing stuff they should not see in a production environment. Quote Link to comment https://forums.phpfreaks.com/topic/135660-advice-on-database-exceptionserrors/#findComment-706814 Share on other sites More sharing options...
franko75 Posted December 5, 2008 Author Share Posted December 5, 2008 Yeah that all makes sense, thanks again. Re the error() function, i take it this could just be a private function inside the database class which you can pass an error to as a parameter (e.g mysql_error() or a custom error)? Quote Link to comment https://forums.phpfreaks.com/topic/135660-advice-on-database-exceptionserrors/#findComment-706827 Share on other sites More sharing options...
premiso Posted December 5, 2008 Share Posted December 5, 2008 Yeah that all makes sense, thanks again. Re the error() function, i take it this could just be a private function inside the database class which you can pass an error to as a parameter (e.g mysql_error() or a custom error)? Yep, except if you have $this->error setup, I would just use that and skip passing the parameter. Quote Link to comment https://forums.phpfreaks.com/topic/135660-advice-on-database-exceptionserrors/#findComment-706829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.