note 62048 added to function.pdostatement-setfetchmode
| From: | matt at kynx dot org | Date: | Sat, 18 Feb 2006 11:13:30 +0000 |
| Subject: | note 62048 added to function.pdostatement-setfetchmode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-103959@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 85.116.0.130
Probable Submitter: 213.55.21.1
----
Manual Page -- http://www.php.net/manual/en/function.pdostatement-setfetchmode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+62048
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62048&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62048&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62048&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62048&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62048&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62048&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62048&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+62048&report=yes
Search -- http://master.php.net/manage/user-notes.php