Jump to content

Hide Errors/warnings


sunnysideup

Recommended Posts

I have a script which gets content from google spreadsheet, but if for some/any reason google docs is not available the script throws warnings as below:

Warning: file() [function.file]: php_network_getaddresses: getaddrinfo failed: No such host is known. in Z:\www\website\scores\indice.php on line 6

Warning: file(http://spreadsheets.google.com/pub?key=reeroigtgf5aHN0ERTS3RxA&single=true&gid=2&range=C47%3AG47&output=csv) [function.file]: failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in Z:\www\website\scores\indice.php on line 6

Warning: Invalid argument supplied for foreach() in Z:\www\website\scores\indice.php on line 12

 

I dont want the visitors to see these errors, is there any way i can hide these errors, or maybe an alternate text comes in like "Sorry, We cannot connect to Data, Please try again Later".

 

There is no error in the script, only when there is a loss of remote connection (eg, if i surf with my LAN off), there error/warnings appear.

 

Please help

 

 

 

Link to comment
Share on other sites

On a live site you would have display_errors set to OFF.

 

maybe an alternate text comes in like "Sorry, We cannot connect to Data, Please try again Later".

Your code should have logic in it to check for errors, output a user error message when an error occurs, and take an appropriate action when there is an error to prevent follow-on errors by preventing the remainder of the code that depends on a previous step from executing (which is what is causing your foreach() error now.)

 

Most of the php functions return a FALSE value when they fail. You should be checking for when that occurs in your code.

 

pseudo code -

if($var = file(.....)){
    // code to execute when file() worked without any errors (i.e. process the data)

} else {
    // application error reporting code to execute when file() failed -
    // output a user error message
   echo "Sorry, We cannot connect to Data, Please try again Later";
}
// remainder of the code on your page having nothing to do with the file() statement

Link to comment
Share on other sites

On a live site you would have display_errors set to OFF.

 

maybe an alternate text comes in like "Sorry, We cannot connect to Data, Please try again Later".

Your code should have logic in it to check for errors, output a user error message when an error occurs, and take an appropriate action when there is an error to prevent follow-on errors by preventing the remainder of the code that depends on a previous step from executing (which is what is causing your foreach() error now.)

 

Most of the php functions return a FALSE value when they fail. You should be checking for when that occurs in your code.

 

pseudo code -

if($var = file(.....)){
    // code to execute when file() worked without any errors (i.e. process the data)

} else {
    // application error reporting code to execute when file() failed -
    // output a user error message
   echo "Sorry, We cannot connect to Data, Please try again Later";
}
// remainder of the code on your page having nothing to do with the file() statement

 

Many Thanks for your replies, I have managed to turn off error by inserting " error_reporting(0); " at the start of the script.  But cant understand how to put a custom message in case of remote site (Googls docs) not available.

 

My File is as below:

<?php
$color1 = "#CFDAFF";  
    $color2 = "#EFF2FF";  
    $row_count = 0; 
    
    
// get the CSV data as an array from the remote URL
error_reporting(0);
$lines = file('http://spreadsheets.google.com/pub?key=reeroigtgf5aHN0ERTS3RxA&single=true&gid=2&range=C47%3AG47&output=csv');

// get rid of header row
//$headers = array_shift($lines);

// Loop through data- therer is only one line hear
foreach ($lines as $line) {
$ldata =  explode(',', trim($line)); // split row to its own array of elements

if ($ldata[0] == '') break; // an empty line means we are done, so exit the foreach loop
$row_color = ($row_count % 2) ? $color1 : $color2;
$row_count++;

      // now we can just output the information as an HTML list, referencing the appropriate array items
      // echo '<li>' . $ldata[0] . '<strong>' . $ldata[1] . '</strong>' . $ldata[2] . '</strong>' . $ldata[3] . '</strong>' . '$ldata[4]' . ''; 
      
?>

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.