RE: [PHP-PEAR] Pear Db layer
| From: | Richard Heyes | Date: | Sat, 30 Sep 2000 14:31:08 +0000 |
| Subject: | RE: [PHP-PEAR] Pear Db layer | ||
| References: | 1 | Groups: | php.pear |
| Request: | Send a blank email to php-pear+get-303@lists.php.net to get a copy of this message | ||
> I'm using PEAR DB classes now, with PHP-4.0.2, PostgreSQL-7.0.2,
> and FreeBSD-4.1.
>
> Example:
<snip>
Thanks, that helped.
After ploughing through the code I've got a couple of points:
1. When doing a query ( $dbh->query() ) it seems to always returns
something valid, either a result object or an error object. So the only
way I can see of testing whether the query was successful is something
like this:
if(get_class($result = $dbh->query('SELECT * FROM users')) ==
'db_result'){
// Success, do stuff.
}else{
die('Something barfed... ');
}
Is there a better way?
2. There didn't appear to be a numRows() method or a affectedRows()
method. If this is just a case of it not having been gotten around to,
there's some code below that works.
This goes in mysql.php:
// {{{ numRows()
/**
* Get the number of rows in a result set.
*
* @param $result MySQL result identifier
*
* @access public
*
* @return int the number of rows in $result
*/
function numRows($result) {
$rows = mysql_num_rows($result);
if (!$rows) {
return new
DB_Error($this->errorCode(mysql_errno($this->connection)));
}
return $rows;
}
// }}}
// {{{ affectedRows()
/**
* Get the number of affected rows by the last query.
*
* @access public
*
* @return int the number of affected rows.
*/
function affectedRows() {
$afrows = mysql_affected_rows($this->connection);
if (!$afrows) {
return new
DB_Error($this->errorCode(mysql_errno($this->connection)));
}
return $afrows;
}
// }}}
And this goes in DB.php:
* Get the the number of rows in a result set.
*
* @return int the number of rows, or a DB error code
*/
function numRows() {
return $this->dbh->numRows($this->result);
}
/**
* Get the the number of affected rows from the last query.
*
* @return int the number of affected rows, or a DB error code
*/
function affectedRows() {
return $this->dbh->affectedRows();
}
/**
Take care.
--
Richard Heyes