steve wrote:
Hi,
In my script (phpnuke), whenever there is access to database, there is
this line of code:
message_die(GENERAL_ERROR, ’some error msg’, ’’, __LINE__, __FILE__,
$sql);
Is there a more elegant way of reporting line number besides putting
this line everywhere I access db. I want to just write a function,
which also globally knows about the current line number(?) and in case
of error reports it.
Is there any way to do that?
One typical way is to first off wrap all of your data access commands in a
single function, this is mine, which hits Postgres:
function SQLExec($sql_command) {
global $dbconn
$errlevel = error_reporting(0);
pg_send_query($dbconn,$sql);
$results=pg_get_result($dbconn);
$t=pg_result_error($results);
if ($t) {
ErrorAdd($t);
ErrorAdd("Command was: $sql");
}
error_reporting($errlevel);
return $results;
}
Another advantage of this is that you are a step closer to platform
independence on the db side.
But for error handling you put this at the top of your dispatcher:
$GLOBALS["errors"]=array();
Now you toss this function at the bottom of the dispatcher. I don't do line
numbers myself in PHP, but if you want them it would be something like
this:
function ErrorAdd($string,$line,$file) {
$GLOBALS["errors"][] =
"Error in $file at line $line: $string";
}
Now give yourself this function:
function Errors() { return count($GLOBALS["errors"]>0; }
which allows to control execution based on existence of prior errors. Some
code maybe needs to run no matter what, and some should not run if there
have been earlier errors.
The icing on the cake is:
function ErrorsHTML() {
if (!Errors()) { return ""; }
$HTML_errs="";
foreach($GLOBALS["error"] as $err) {
$HTML_errs.=$err."<br>\n";
}
return '<div class="errors">'.$HTML_errs.'</div>';
}
so you can now put the following unconditional code at the top of each page,
or in your dispatcher:
<?php echo ErrorsHTML(); ?>
....then you move on to other things in life.
Hope this helps.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)