Re: [PHP-PEAR] DB_mysql Bugs?

[email protected] (Chuck Hagenbuch)
Newsgroups php.pear
Message-ID <[email protected]>
Quoting Vinai Kopp <[email protected]>:

> 1.) This one is close to the preceding one. The way I see it a bug was
> introduced in Revision 1.44 of DB.php.
> The function isError now returns true if NULL is passed as an argument.
> That way it's not possible to do 
>       if( DB::isError( $var ) ) print $var->getMessage();
> anymore without doing another is_object or something first. Also, it
> would make it impossible to return NULL if no more data can be fetched
> from a query in fetchRow and similar functions. I suggest to change it
> back.

Okay, I'll elaborate a bit more on this. I changed DB::isError() to flag NULL 
as an error because some of the db backends were returning NULL at the end of a 
result set, and some were returning an error object. If you want to be able to 
write a while loop that will terminate on an error or at the end of a result 
set, you need a single check that will tell you if either occurred.

If we could make objects evaluate to false, then we could make DB_error objects 
evaluate to false and we'd have a simple check. If we had exceptions, we could 
throw an exception and just check for null, and we'd have a simple check. But 
as it is, you'd need:

$row = $result->fetchRow();
while (($row != null) && !DB::isError($row)) {
    // stuff
    $row = $result->fetchRow();
}

... if you wanted null at the end of a result set (I agree that this makes the 
most sense, yes), and DB::isError didn't consider null an error.

To me, that's an awkward thing to force people to do. Maybe all this could be 
solved by a new function: DB::isResult().

while (DB::isResult($row = $result->fetchRow())) {
    // stuff
}
if (DB::isError($row)) {
    // error
}

You still need a second check to see if you have an error - but without 
exceptions, there's no way around that.

-chuck

--
Charles Hagenbuch, <[email protected]>
"My intuitive grasp of math often leads me astray." -Me
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.