Content · Programming · PHP

PHP pg_fetch_array() Function: Usage and Examples

Learn pg_fetch_array() with clear PHP examples, output, practical scenarios, important behaviors, and common beginner mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP developers sometimes need to fetch a PostgreSQL query row as an associative, numeric or combined array. This comes up when preparing data, displaying results, or integrating information with other services.

The pg_fetch_array() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.

What Is pg_fetch_array() in PHP?

Fetch a PostgreSQL query row as an associative, numeric or combined array.

Its syntax is:

pg_fetch_array(PgSql\Result $result, ?int $row = null, int $mode = PGSQL_BOTH): array

The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.

Important: When no matching row remains, returns false. Passing a row number reads that row directly; use pg_query_params for external values.

Parameters:

  • $result — A named input or configuration argument; use the declared type and optional default shown above.
  • $row — A named input or configuration argument; use the declared type and optional default shown above.
  • $mode — A named input or configuration argument; use the declared type and optional default shown above.

Example 1: Basic Usage

This first example uses a small input to demonstrate the main result of pg_fetch_array().

<?php

$connection = pg_connect("host=localhost dbname=demo user=app password=secret"); $result = pg_query($connection, "SELECT id, name FROM people LIMIT 2"); while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) { echo $row["name"] . "\n"; }

Output: The result depends on an installed extension, an existing file, database records, or a randomly selected order. The code is an illustrative example and requires the environment described below.

The output demonstrates the primary operation: fetch a PostgreSQL query row as an associative, numeric or combined array.

Example 2: Understanding Another Case

This second example changes the input or option to reveal a different behavior of pg_fetch_array().

<?php

$connection = pg_connect("host=localhost dbname=demo user=app password=secret"); $result = pg_query($connection, "SELECT id, name FROM people LIMIT 1"); print_r(pg_fetch_array($result, 0, PGSQL_NUM));

Output: The result depends on an installed extension, an existing file, database records, or a randomly selected order. The code is an illustrative example and requires the environment described below.

Compared with the first example, this one illustrates the same operation with a different input shape or option. When no matching row remains, returns false. Passing a row number reads that row directly; use pg_query_params for external values.

Practical Example: A Real Application Scenario

Here is a practical use of pg_fetch_array() with realistic sample information. Adapt the values to match your application.

<?php

$connection = pg_connect("host=localhost dbname=demo user=app password=secret"); $result = pg_query_params($connection, "SELECT name FROM people WHERE id = $1", [5]); while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) { echo htmlspecialchars($row["name"], ENT_QUOTES, "UTF-8"); }

Output: The result depends on an installed extension, an existing file, database records, or a randomly selected order. The code is an illustrative example and requires the environment described below.

This is a useful starting point, but production code should check errors and validate values before performing the operation. When no matching row remains, returns false. Passing a row number reads that row directly; use pg_query_params for external values.

Important Behavior and Edge Cases

When no matching row remains, returns false. Passing a row number reads that row directly; use pg_query_params for external values.

  • Input: Use the expected PHP types and validate user-provided values.
  • Result: Check the return value separately from any modifications to the input.
  • Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.

Comparison With Related Functions

Function or ApproachWhen to Use It
pg_fetch_array()Fetch a PostgreSQL query row as an associative, numeric or combined array.
Related functionspg_fetch_assoc() only uses column names; pg_fetch_row() only numeric positions.

Common Beginner Mistakes

MistakeCorrect Approach
Building SQL by concatenating inputBind data values through prepared statements; do not interpolate user data into SQL.
Assuming a live database is availableConfigure the required driver, schema, and connection, then check query errors.
Confusing raw COPY data with fetched rowsWhen no matching row remains, returns false. Passing a row number reads that row directly; use pg_query_params for external values.

Key Takeaways

  • pg_fetch_array() lets you fetch a PostgreSQL query row as an associative, numeric or combined array.
  • When no matching row remains, returns false. Passing a row number reads that row directly; use pg_query_params for external values.
  • pg_fetch_assoc() only uses column names; pg_fetch_row() only numeric positions.
  • Check input, return values, and environmental requirements when adapting the examples.

pg_fetch_array() provides a focused PHP operation for fetch a PostgreSQL query row as an associative, numeric or combined array. Start with the simple examples, then adapt the practical example with the correct checks for your project.

Share this resourceSend this page to someone who may find it useful.