slarson20 Posted September 9, 2011 Share Posted September 9, 2011 Whats the best way to handle database errors. I been using ob_start() stuff to run the code and if a database connection error occurs an variable $error turns to 1 and it displays the error code instead of the normal page that should be displayed when no errors occur. It works but I want to know if there is any better way to manage a db connection error while running a script. Also, how do you manage a problem when like 3 queries are ran to setup a new user account and the last one fails because the db connection drops or something, how would you go about handling a 2/3 query successful? I'm really stuck on how to manage errors if a db connection fails after the first connection is successful. Quote Link to comment https://forums.phpfreaks.com/topic/246816-handling-db-errors/ Share on other sites More sharing options...
doddsey_65 Posted September 9, 2011 Share Posted September 9, 2011 when running a query use an "or die()" statement. If there is an error the script will abort and the error message aswell as a line number will be displayed. Example: $sql = ""; mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/246816-handling-db-errors/#findComment-1267494 Share on other sites More sharing options...
Pikachu2000 Posted September 9, 2011 Share Posted September 9, 2011 You would not want to do that on a live site, however. You would want to log the actual error, and just show the user a generic "Sorry, the database puked. Try later" type of message. Quote Link to comment https://forums.phpfreaks.com/topic/246816-handling-db-errors/#findComment-1267497 Share on other sites More sharing options...
slarson20 Posted September 9, 2011 Author Share Posted September 9, 2011 Lets say you use a template object to get the title, and that query fails, then what? Quote Link to comment https://forums.phpfreaks.com/topic/246816-handling-db-errors/#findComment-1267503 Share on other sites More sharing options...
Psycho Posted September 9, 2011 Share Posted September 9, 2011 Lets say you use a template object to get the title, and that query fails, then what? I will sometimes build sites with different "levels" of errors. Some errors are minor and you can display the page as normal with the error displayed. However, there are also critical errors that prevent you from even gathering enough data to put together a "pretty" error page. For the latter you could have a flat HTML error page when that occurs. I have noticed that this site display an all white page with a short message when DB connectivity is down. Since it cannot access the database there isn't much use in trying to show the normal page with the pretty colors and such. Even something such as the menu at the top would be dynamic based on the user's permissions (which are stored in the DB). So, make a decision on how you want to handle. If you can't pull a template do you even continue trying to display a "pretty" page, do you use a default template, or what? Quote Link to comment https://forums.phpfreaks.com/topic/246816-handling-db-errors/#findComment-1267512 Share on other sites More sharing options...
Pikachu2000 Posted September 9, 2011 Share Posted September 9, 2011 For the html <title></title> attribute? That's pretty insignificant as far as usability is concerned, so if it was me, I'd just check the variable and if it was empty, display a default title. Quote Link to comment https://forums.phpfreaks.com/topic/246816-handling-db-errors/#findComment-1267513 Share on other sites More sharing options...
slarson20 Posted September 10, 2011 Author Share Posted September 10, 2011 So basically all you can do is set defaults, and handle the error when it occurs. So using ob functions to make sure its fully error free before displaying is bad? Quote Link to comment https://forums.phpfreaks.com/topic/246816-handling-db-errors/#findComment-1267543 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.