New fetchmode DB_FETCHMODE_OBJECT avaible
| From: | Tomas V.V.Cox | Date: | Fri, 03 Aug 2001 14:39:58 +0000 |
| Subject: | New fetchmode DB_FETCHMODE_OBJECT avaible | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-1272@lists.php.net to get a copy of this message | ||
Hi,
By popular demand, a new fetchmode DB_FETCHMODE_OBJECT is avaible. This
fetchmode will return the column data of a fetched row as properties of
a new object DB_row. For example:
$sql = 'select id, name from table where id=1';
$row = $db->getRow($sql, DB_FETCHMODE_OBJECT);
print_r($row);
// The default class type of the row object is DB_row
db_row Object
(
[id] => 1
[name] => seven
[upper] => SEVEN
[date] => 02-25-2001
)
So you can access the data with:
$id = $row->id;
$name = $row->name;
This is not all. Now setFetchMode accepts a new option param: "the class
type of the row object to return". This allows developers to extend the
functionality of the row object. Here is an example:
<?php
require_once 'DB.php';
class Money_row extends DB_row
{
function Money_row(&$array) {
$this->DB_row($array);
}
function client_name() {
return $this->name . $this->surname;
}
}
?>
And use it in your code:
require_once 'Money_row.php';
$db = DB::connect($dsn);
$db->setFetchMode(DB_FETCHMODE_OBJECT, 'Money_row');
$res = $db->query('select * from clients');
while ($row = $res->fetchRow()) {
echo $row->client_name();
}
Isn't pear db nice? :)
Tomas V.V.Cox