pg_fetch_array

(PHP 4, PHP 5, PHP 7, PHP 8)

pg_fetch_arrayFetch a row as an array

Description

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

pg_fetch_array() returns an array that corresponds to the fetched row (record).

pg_fetch_array() is an extended version of pg_fetch_row(). In addition to storing the data in the numeric indices (field number) to the result array, it can also store the data using associative indices (field name). It stores both indices by default.

Note: This function sets NULL fields to the PHP null value.

pg_fetch_array() is NOT significantly slower than using pg_fetch_row(), and is significantly easier to use.

Parameters

result

An PgSql\Result instance, returned by pg_query(), pg_query_params() or pg_execute()(among others).

row

Row number in result to fetch. Rows are numbered from 0 upwards. If omitted or null, the next row is fetched.

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.

Return Values

An array indexed numerically (beginning with 0) or associatively (indexed by field name), or both. Each value in the array is represented as a string. Database NULL values are returned as null.

false is returned if row exceeds the number of rows in the set, there are no more rows, or on any other error. Fetching from the result of a query other than SELECT will also return false.

Changelog

Version Description
8.1.0 The result parameter expects an PgSql\Result instance now; previously, a resource was expected.

Examples

Example #1 pg_fetch_array() example

<?php 

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

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

$arr pg_fetch_array($result0PGSQL_NUM);
echo 
$arr[0] . " <- Row 1 Author\n";
echo 
$arr[1] . " <- Row 1 E-mail\n";

// The row parameter is optional; NULL can be passed instead,
// to pass a result_type.  Successive calls to pg_fetch_array
// will return the next row.
$arr pg_fetch_array($resultNULLPGSQL_ASSOC);
echo 
$arr["author"] . " <- Row 2 Author\n";
echo 
$arr["email"] . " <- Row 2 E-mail\n";

$arr pg_fetch_array($result);
echo 
$arr["author"] . " <- Row 3 Author\n";
echo 
$arr[1] . " <- Row 3 E-mail\n";

?>

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial