Re: Pear DB Tutorial
| From: | Tomas V.V.Cox | Date: | Mon, 16 Apr 2001 23:00:42 +0000 |
| Subject: | Re: Pear DB Tutorial | ||
| References: | 1 2 3 4 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-242@lists.php.net to get a copy of this message | ||
Stig Sæther Bakken wrote:
>
> ["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
>
Before setErrorHandling you have to check if $dbh is not an error. If it
is an error you don't have setErrorHandle and if it isn't you don't need
it :)
$dbh = DB::connect($dsn);
if (DB::isError($dbh)) {
handle_error ($dbh);
}
Perhaps your example works better with results objects:
$res = $dbh->query($sql);
if (DB::isError($res)) {
handle_error ($res);
} else {
$res->setErrorHandling(PEAR_ERROR_CALLBACK, "handle_error");
...
}
But there is still to much code to write (the matter of this thread).
Perhaps if we propagate the error handling mode throw the new results
objects can work better. Any way, me, my self and I prefer the way I
originally post ;)
>
> The same method can be used for any class that inherits PEAR. It
> appears some more documentation (and maybe some evangelism?) would
> help? :-)
Hey your are our prophet! :) In the next days I'll try to extend the
Tutorial.
Tomas V.V.Cox