visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Databases :: MySql :: redirect to an error page in case of mysql error


By: Bernard France  Date: 10/06/2007 08:49:54  English  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 English  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 English  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

EContact
browser fav
page generated in 336.502790 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page