pdostatement::fetchall-凯发官网入口
pdostatement::fetchall
(php 5 >= 5.1.0, php 7, php 8, pecl pdo >= 0.1.0)
pdostatement::fetchall — 从结果集中获取剩余的行
说明
public pdostatement::fetchall(int $mode = pdo::fetch_default): array
public pdostatement::fetchall(int $mode = pdo::fetch_column, int $column): array
public pdostatement::fetchall(int $mode = pdo::fetch_class, string $class, ?array $constructorargs): array
public pdostatement::fetchall(int $mode = pdo::fetch_func, callable $callback): array
参数
mode控制返回数组的内容如同 pdostatement::fetch() 文档中记载的一样。默认为
pdo::attr_default_fetch_mode的值( 其缺省值为pdo::fetch_both)想要返回一个包含结果集中单独一列所有值的数组,需要指定
pdo::fetch_column。通过指定column参数获取想要的列。想要获取结果集中单独一列的唯一值,需要将
pdo::fetch_column和pdo::fetch_unique按位或。想要返回一个根据指定列把值分组后的关联数组,需要将
pdo::fetch_column和pdo::fetch_group按位或。
以下是依赖获取模式的动态参数。它们不能与命名参数一起使用。
column与
pdo::fetch_column一起使用。返回指定以 0 开始索引的列。class与
pdo::fetch_class一起使用。返回指定类的实例,映射每行的列到类中对应的属性名。constructorargs当
mode参数为pdo::fetch_class时自定义类构造方法的参数。callback与
pdo::fetch_func一起使用。将每行的列作为参数传递给指定的函数,并返回调用函数后的结果。
返回值
pdostatement::fetchall() 返回一个包含结果集中所有剩余行的数组。此数组的每一行要么是一个列值的数组,要么是属性对应每个列名的一个对象。如果获取到的结果为 0,则返回空数组。
使用此方法获取大结果集将导致系统负担加重且可能占用大量网络资源。与其取回所有数据后用php来操作,倒不如考虑使用数据库服务来处理结果集。例如,在取回数据并通过php处理前,在 sql 中使用 where 和 order by 子句来限定结果。
错误/异常
如果属性 pdo::attr_errmode 设置为 pdo::errmode_warning,则发出级别为 e_warning 的错误。
如果属性 pdo::attr_errmode 设置为 pdo::errmode_exception,则抛出 pdoexception。
更新日志
| 版本 | 说明 |
|---|---|
| 8.0.0 | 现在此方法始终返回 array,之前可能在失败时返回 false。 |







