Re: PEAR DB examples?
| From: | Stig S. Bakken | Date: | Fri, 08 Sep 2000 23:36:02 +0000 |
| Subject: | Re: PEAR DB examples? | ||
| References: | 1 | Groups: | php.pear |
| Request: | Send a blank email to php-pear+get-241@lists.php.net to get a copy of this message | ||
On Fri, 8 Sep 2000, Sterling Hughes wrote:
| > On Fri, 08 Sep 2000, Sterling Hughes wrote:
| > > <?php
| > > $dbh = DB::connect("mysql://user:pass@server/dbname");
| > >
| > > if (DB::isError($dbh)) {
| > > die(sprintf("Error [%d]: %s",
| > > $dbh->getCode(), $dbh->getMessage()));
| > > }
| > >
| > > $sth1 = $dbh->query("select article_id from articles where author_id = 1");
| > >
| > > // .. same error checks apply to queries
| > >
| > > while (is_array($row = $sth1->fetchRow())) {
| > > $sth2 = $dbh->query("select image_id from images where article_id = " .
| > > $row[article_id]);
| > > //...
| > > }
| > > ?>
| > >
| >
| > Hmm, I see. It might get a little annoying to write error checks after
| > each query - is there a way to automate that a bit?
| >
| > -Andrei
| >
|
| Perhaps we could have a register_error_function method:
|
| function error ($obj) {
| die(sprintf("Error [%d]: %s",
| $obj->getCode(), $obj->getMessage()));
| }
|
| $dbh->register_error_function("error");
|
| // .. Normal stuff, the error function will be called on error. No need to
| check
I've been pondering a bit on this one, and the solution that I find the
most appealing is the one you suggest. Examples:
$dbh->setErrorHandling(PEAR_ERROR_RETURN);
$dbh->setErrorHandling(PEAR_ERROR_PRINT);
$dbh->setErrorHandling(PEAR_ERROR_TRIGGER);
$dbh->setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_WARNING);
$dbh->setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_ERROR);
$dbh->setErrorHandling(PEAR_ERROR_DIE);
$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, "error_function");
$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, array(&$obj, "error_method"));
- Stig