User's Manual
Adding Error Recovery
Beta Draft Updating Data 5-15
Most production systems would display_errors configuration option in the
php.ini file set off, and log_errors set on.
PHP's output buffering functionality can be used to trap error text during a function.
Using ob_start() prevents text from displaying on the screen. If an error occurs
ob_get_contents() allows the previously generated error messages to be stored in
a string for later display or analysis.
Here we'll change the application so error and database errors are displayed on a new
page using a customer error handling function. Errors are now returned from the db*
functions keeping them silent.
1. Edit anyco_db.inc. Change db_error() to return the error information in an
array structure, instead of printing and quitting.:
function db_error($r = false, $file, $line)
{
$err = $r ? oci_error($r) : oci_error();
if (isset($err['message'])) {
$m = htmlentities($err['message']);
$c = $err['code'];
}
else {
$m = 'Unknown DB error';
$c = null;
}
$rc = array(
'MESSAGE' => $m,
'CODE' => $c,
'FILE' => $file,
'LINE' => $line
);
return $rc;
}
2. Edit anyco_db.inc. For every call to db_error(), assign the return value to a
variable called $e and add a return false; statement after each call:
if (<error test>)
{
$e = db_error(<handle>, __FILE__, __LINE__);
return false;
}
Make sure to keep the <error test> and <handle> parameters the same as
they are currently specified for each call. Remember the __FILE__ and __LINE__
constants help pinpoint the location of the failure during development. This is
useful information to log for fatal errors in a production deployment of an
application.
3. Edit anyco_db.inc. Add a $e parameter to every function to enable the return
of error information. Use the & reference prefix to ensure that results are returned
to the calling function. Each function declaration becomes:
function db_connect(&$e) {...}
function db_get_page_data($conn, $q1, $currrownum = 1, $rowsperpage = 1,
&$e, $bindvars = array()) {...}
function db_do_query($conn, $statement, $resulttype, &$e,