sunnysideup Posted April 21, 2010 Share Posted April 21, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199249-hide-errorswarnings/ Share on other sites More sharing options...
Pikachu2000 Posted April 21, 2010 Share Posted April 21, 2010 You can disable error reporting, but if this is being just being tested on your local machine, I'd leave it alone. If it's on a live site, you can edit the php.ini file to disable error reporting and enable error logging instead. Quote Link to comment https://forums.phpfreaks.com/topic/199249-hide-errorswarnings/#findComment-1045750 Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2010 Share Posted April 21, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199249-hide-errorswarnings/#findComment-1045761 Share on other sites More sharing options...
sunnysideup Posted April 21, 2010 Author Share Posted April 21, 2010 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]' . ''; ?> Quote Link to comment https://forums.phpfreaks.com/topic/199249-hide-errorswarnings/#findComment-1045789 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.