大网数据广告活动
PHP 语言与运行手册

pg_fetch_all · 从结果中获取所有行作为数组

· 52CE 我爱测

pg_fetch_all() 返回包含 PgSql\Result 实例中所有行(记录)的数组。

pg_fetch_all

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

pg_fetch_all — 从结果中获取所有行作为数组

说明

function pg_fetch_all(PgSql\Result $result, int $mode = PGSQL_ASSOC): array

pg_fetch_all() 返回包含 PgSql\Result 实例中所有行(记录)的数组。

注意:

此函数将 NULL 字段设置为 PHP null 值。

参数

result

PgSql\Result 实例,由 pg_query()、pg_query_params() 或者 pg_execute()(等)返回。

mode

An optional parameter that controls how the returned array is indexed. mode is a constant and can take the following values: PGSQL_ASSOC, PGSQL_NUM and PGSQL_BOTH. Using PGSQL_NUM, the function will return an array with numerical indices, using PGSQL_ASSOC it will return only associative indices while PGSQL_BOTH will return both numerical and associative indices.

返回值

包含结果中所有行的 array。每行都是一个由字段名作为索引的字段值数组。

更新日志

版本 说明
8.1.0 现在 result 参数接受 PgSql\Result 实例,之前接受 resource。
8.0.0 对于零行的结果集,pg_fetch_all() 现在将返回一个空数组而不是 array。
7.1.0 新增 mode 参数。

示例

示例 #1 PostgreSQL 获取全部

<?php 
$conn = pg_pconnect("dbname=publisher");
if (!$conn) {
    echo "An error occurred.\n";
    exit;
}

$result = pg_query($conn, "SELECT * FROM authors");
if (!$result) {
    echo "An error occurred.\n";
    exit;
}

$arr = pg_fetch_all($result);

print_r($arr);

?>

以上示例的输出类似于:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Fred
        )

    [1] => Array
        (
            [id] => 2
            [name] => Bob
        )

)

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。