The pgsql extension retrieves rows as objects or arrays from a PostgreSQL query result.
What Is pg_fetch_object in PHP?
Fetches the next PostgreSQL result row as an object, returning false when no further row is available.
Simplified call syntax (optional overload parameters are omitted where applicable):
pg_fetch_object(PgSql\Result $result, ?int $row = null): object|false
$result— The PostgreSQL query-result resource/object returned by the pgsql extension.$row— Optional row number, or null to read the next row.
Example 1: Understanding the Basic Behavior
<?php
$connection = pg_connect('host=127.0.0.1 dbname=sample user=app password=example');
$result = pg_query($connection, "SELECT 7 AS id, 'Alex' AS name");
$record = pg_fetch_object($result);
echo $record->name;
Requires a working PostgreSQL connection and the pgsql extension.
Explanation: Fetches the next PostgreSQL result row as an object, returning false when no further row is available.
Example 2: Testing Another Case
<?php
$connection = pg_connect('host=127.0.0.1 dbname=sample user=app password=example');
$result = pg_query($connection, 'SELECT 1 AS id UNION ALL SELECT 2');
while ($record = pg_fetch_object($result)) { echo $record->id . '\n'; }
Requires a working PostgreSQL connection and the pgsql extension.
Explanation: The result requires a successful PostgreSQL query and an installed pgsql extension.
Practical Example: Applying the Operation
<?php
$connection = pg_connect('host=127.0.0.1 dbname=sample user=app password=example');
$result = pg_query_params($connection, 'SELECT $1::text AS name', ['Morgan']);
if ($result !== false && ($record = pg_fetch_object($result))) { echo htmlspecialchars($record->name, ENT_QUOTES, 'UTF-8'); }
Requires a working PostgreSQL connection and the pgsql extension.
Explanation: The column names become object properties; false means no row remains.
Important Behavior and Edge Cases
- The result requires a successful PostgreSQL query and an installed pgsql extension.
- The column names become object properties; false means no row remains.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
pg_fetch_object | Fetches the next PostgreSQL result row as an object, returning false when no further row is available. |
pg_fetch_assoc() | pg_fetch_object() returns an object representing a row; pg_fetch_assoc() returns an associative array. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating a false end-of-results value as a valid row object. | The result requires a successful PostgreSQL query and an installed pgsql extension. |
| Choosing pg_fetch_assoc() without checking whether it provides the same result. | pg_fetch_object() returns an object representing a row; pg_fetch_assoc() returns an associative array. |
Key Takeaways
- Fetches the next PostgreSQL result row as an object, returning false when no further row is available.
- The result requires a successful PostgreSQL query and an installed pgsql extension.
- The column names become object properties; false means no row remains.
The examples demonstrate where pg_fetch_object fits within database result handling. Verify the relevant preconditions whenever you adapt the code to real data.