简体   繁体   中英

Fetching all rows as Object with pdo php

I know that the function fetchObject ( http://www.php.net/manual/en/pdostatement.fetchobject.php ) gives me the next row as an object of the specified class, but I want to get all the rows as an object of the specified class, does PDO has some function to this or I have to do it manually???

THanks!!

You are looking for PDOStatement::fetchAll :

PDOStatement::fetchAll — Returns an array containing all of the result set rows

Example usage:

$arr = $stmt->fetchAll(PDO::FETCH_CLASS, $class_name, $constructor_args);

If you do not need all of the objects in a single array, you will probably find iterating through all the rows to work just as well:

while ($obj = $stmt->fetchObject($class_name, $constructor_args)) {
    // Process $obj
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM