php8.5
Home/ Manual/ pgsql / functions/ pg_fetch_all

pg_fetch_all

PHP function Edit on GitHub ✎

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

Fetches all rows from a result as an array

Description

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

pg_fetch_all() returns an array that contains all rows (records) in the PgSql\Result instance.

Fetch-null

Parameters

result

Result

mode

Mode

Return Values

An array with all rows in the result. Each row is an array of field values indexed by field name.

Changelog

VersionDescription
8.0.0pg_fetch_all() will now return an empty Array instead of false for result sets with zero rows.
7.1.0The mode parameter was added.

Examples

PostgreSQL fetch all

php
<?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);

?>

The above example will output something similar to:

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

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

)

See Also

Source: reference/pgsql/functions/pg-fetch-all.xml · from the official PHP manual (php/doc-en)