Re: Pear DB Tutorial
| From: | (Stig Sæther Bakken) | Date: | Mon, 16 Apr 2001 22:28:40 +0000 |
| Subject: | Re: Pear DB Tutorial | ||
| References: | 1 2 3 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-240@lists.php.net to get a copy of this message | ||
["Tomas V.V.Cox" <cox@idecnet.com>]
> Andrei Zmievski wrote:
> >
> > Has anyone written a simple wrapper around PEAR DB stuff? I don't want
> > to do DB::isError() after every method call, pretty much. Having it
> > simplified would be nice.
> >
>
> I can think a simple "error filter" system so (code not tested):
>
> <?php
> // what kind of error message to output
> define (DEBUG_ENV, true);
>
> function handle_error (&$res) {
> if (DB::isError($res)) {
> // here you can set the default actions to take
> // or set pear error handling
> if (DEBUG_ENV) {
> die($res->getDebugInfo());
> }
> die ($res->getMessage());
> }
> return $res;
> }
>
> $db = handle_error(DB::connect($dsn));
> $res = handle_error($db->query($sql));
> $numrows = handle_error($res->numRows());
> ?>
Hey hey hey, you don't all have to go out and re-invent the wheel. :-)
There is a way to do this already, with a default callback error
handler:
<?php
function handle_error($obj) {
print "<H1><BLINK>" . $obj->getMessage() .
"</BLINK></H1>";
print "</BODY></HTML>\n";
exit;
}
$dbh = DB::connect($dsn);
$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, "handle_error");
// from now you can assume that all DB calls succeed, those who
// don't make the script exit
?>
The same method can be used for any class that inherits PEAR. It
appears some more documentation (and maybe some evangelism?) would
help? :-)
- Stig
--
Stig Sæther Bakken <ssb@fast.no>
Fast Search & Transfer ASA, Trondheim, Norway