pg_fetch_array
(PHP 4, PHP 5, PHP 7)
pg_fetch_array — Fetch a row as an array
Description
$result
[, int $row
[, int $result_type
= PGSQL_BOTH
]] ) : arraypg_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 indicies 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
-
PostgreSQL query result resource, 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. -
result_type
-
An optional parameter that controls how the returned array is indexed.
result_type
is a constant and can take the following values:PGSQL_ASSOC
,PGSQL_NUM
andPGSQL_BOTH
. UsingPGSQL_NUM
, pg_fetch_array() will return an array with numerical indices, usingPGSQL_ASSOC
it will return only associative indices whilePGSQL_BOTH
, the default, 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.
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($result, 0, PGSQL_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($result, NULL, PGSQL_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
- pg_fetch_row() - Get a row as an enumerated array
- pg_fetch_object() - Fetch a row as an object
- pg_fetch_result() - Returns values from a result resource
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.pg-fetch-array.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.