PDOStatement::setFetchMode()
reset default fetch() mode for a statement to PDO_FETCH_BOTH
PDOStatement::setFetchMode(PDO_FETCH_NUM)
PDOStatement::setFetchMode(PDO_FETCH_ASSOC)
PDOStatement::setFetchMode(PDO_FETCH_BOTH)
PDOStatement::setFetchMode(PDO_FETCH_OBJ)
set default fetch() mode for a statement.
PDOStatement::setFetchMode(PDO_FETCH_COLUMN, int colno)
set default fetch() mode to retrieve colno-th column on each fetch() call.
PDOStatement::setFetchMode(PDO_FETCH_CLASS, string classname [, array ctor args])
set default fetch() mode to create an instance of classname,
calling it's ctor, passing the optional ctor args.
The names of the columns in the result set will be used as property names on
the object instance. PPP rules apply.
[NOTE: calling ctor is not yet implemented]
[TODO: this might crash PHP for persistent PDO handles]
PDOStatement::setFetchMode(PDO_FETCH_INTO, object obj)
Similar to PDO_FETCH_CLASS, except that each iteration will update the
supplied object properties.
[TODO: this might crash PHP for persistent PDO handles]
The default fetch() mode is used when no parameters are passed to
PDOStatement::fetch(). When using a statement in an iterator context,
PDOStatement::fetch() is called implicitly on each iteration.
object PDO::queryAndIterate(string sql, <PDOStatement::setFetchMode args>)
This is semantically equivalent to:
$stmt = $pdo->prepare($sql);
$stmt->execute();
$stmt->setFetchMode($args);
return $stmt;
Example/Intended usage:
/* fetch an array with numeric and string keys */
foreach ($pdo->queryAndIterate("select NAME, VALUE from test") as $row) {
debug_zval_dump($row);
}
/* fetch the value of column 1 into $row on each iteration */
foreach ($pdo->queryAndIterate("select NAME, VALUE from test",
PDO_FETCH_COLUMN, 1) as $row) {
debug_zval_dump($row); // string(3) "foo"
}
/* create a new instance of class Foo on each iteration */
foreach ($pdo->queryAndIterate("select NAME, VALUE from test",
PDO_FETCH_CLASS, 'Foo') as $row) {
debug_zval_dump($row);
/*
Object(Foo)#4 (2) refcount(2){
["NAME"]=>
string(12) "foo220051429" refcount(2)
["VALUE"]=>
string(12) "bar789825748" refcount(2)
}
*/
}
etc.
I have cooked a small patch which allows is_subclass_of() the accept
not only an object as first parameter but a string as well. When string
is passed the function checks whether the class specified is subclass of
the second parameter
class a{}
class b{} extends a{}
is_subclass_of("a", "a") //false
is_subclass_of("b", "a") //true
currently only objects are allowed as first parameter