Jump to content

Clear output to show error page


techpro

Recommended Posts

I'm running a PHP/MySQL script on my server. Sometimes, presumably due to some transient problem with the MySQL server, the script displays warnings like:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/blah_blah/file.php on line xx

 

on the page. It's not a script error, because the same page can display perfectly a couple of minutes later.

 

I wanted to have the script display a friendly page saying there is a temporary error, please come back later, when this occurs. With the help of this article I have learned how to trap PHP errors and display my own output. But the output still appears after whatever HTML has already been generated by the script.

 

Is there a way to get the browser to clear everything that has been output so my error message always starts on a clean page? Rewriting the script so that nothing is printed until the end would be way too complicated.

 

Thanks in advance.

Link to comment
Share on other sites

I usually gather all of the errors on my page using an array.

 

<?php

$errors = array();

if($something){
$errors[] = "An error has occured, unable to fetch info from the database";
}

if(empty($errors)){
//do the right thing
}else{
//display error page
}
?>

Link to comment
Share on other sites

Thanks. I didn't write the script. I'm trying to find a solution that requires minimum modification to the existing code, preferably just adding an include line at the top for the error handler. So I can't do anything about what's been output to the browser by the time an error occurs.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.