To fetch the rows into an existing instance of a class, use PDO::FETCH_INTO and pass the object as the second parameter.
The class _must_ have the column names declared as public members, or the script will die. But overloading with __set() and __get() lets it handle any column your query throws at it.
Set the fetch mode right after you call prepare(). It appears you _must_ use execute() - fetch() won't work. A small example, adapted from ext/pdo/tests/pdo_025.phpt:
<?php
class Test
{
protected $cols;
function __set($name, $value) {
$this->cols[$name] = $value;
}
function __get($name) {
return $this->cols[$name];
}
}
$obj = new Test();
$db = PDOTest::factory();
$stmt = $db->prepare("select * from test");
$stmt->setFetchMode(PDO::FETCH_INTO, $obj);
$stmt->execute();
foreach ($stmt as $a) {
print_r($a);
}
print_r($obj); // contains the same values as the last iteration above
?>