简体   繁体   English

使用PDO PHP将所有行作为对象获取

[英]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??? 我知道函数fetchObject( http://www.php.net/manual/zh/pdostatement.fetchobject.php )给我下一行作为指定类的对象,但是我想将所有行作为指定类的对象,PDO是否对此具有某些功能,或者我必须手动执行?

THanks!! 谢谢!!

You are looking for PDOStatement::fetchAll : 您正在寻找PDOStatement::fetchAll

PDOStatement::fetchAll — Returns an array containing all of the result set rows PDOStatement::fetchAll —返回包含所有结果集行的数组

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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