Databases :: MySql :: redirect to an error page in case of mysql error |
|||
| By: Bernard |
Date: 10/06/2007 08:49:54 |
Points: 20 | Status: Answered Quality : Excellent |
| How to redirect to an error page in case of mysql error ? | |||
| By: VGR | Date: 11/06/2007 16:41:05 | Type : Answer |
|
| relatively easy. First, there are at least three solutions to actually do the redirection. The best (cleaner?) one is to use the proper HTTP header call. Halas, this requires that DHTML output has not yet begun. Thus you've to ensure that not a single charcarter has been echoed, including (mis-)leading blanks left hanging around your PHP code. You've two solutions to reach that : either carefully check all your pages (just do a "view page source" in your browser) to be sure no whitespaces, blanks, CRLFs, etc mangle your output before reaching the redirect place, ***or*** (for sure the most efficient and time-saving solution) use systematically "output buffering" in your scripts. I will only show you the best combination IMHO : <?php $query="update yourtable set status=9 where id=$id"; $result=@mysql_query($query,$linkId); if ($result===FALSE) { // a DB error occured, redirect to error page $theerror=mysql_error(); LogAction("the error '$theerror' occured"); // this is optional, just a suggestion $warningmsg=urlencode($theerror); header("Location: error_display_page.php?warningmsg=$warningmsg"); exit; } else { // no error occured, proceed... // rest of the DB handling code here } ?> That's all, best regards |
|||
| By: VGR | Date: 11/06/2007 16:42:47 | Type : Comment |
|
| I forgot to explain the output buffering. Just add ob_start(); on top of your commonly included file, or on top of each script. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








