PHP: Hypertext Preprocessor (original) (raw)

pg_fetch_row

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

pg_fetch_row — Get a row as an enumerated array

Description

Note: This function sets NULL fields to the PHP [null](reserved.constants.php#constant.null) value.

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](reserved.constants.php#constant.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.constants.php#constant.pgsql-assoc), [PGSQL_NUM](pgsql.constants.php#constant.pgsql-num) and [PGSQL_BOTH](pgsql.constants.php#constant.pgsql-both). Using [PGSQL_NUM](pgsql.constants.php#constant.pgsql-num), the function will return an array with numerical indices, using [PGSQL_ASSOC](pgsql.constants.php#constant.pgsql-assoc) it will return only associative indices while [PGSQL_BOTH](pgsql.constants.php#constant.pgsql-both) will return both numerical and associative indices.

Return Values

An array, indexed from 0 upwards, with each value represented as a string. Database NULL values are returned as [null](reserved.constants.php#constant.null).

[false](reserved.constants.php#constant.false) is returned if row exceeds the number of rows in the set, there are no more rows, or on any other error.

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_row() 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;
}

while ( row=pgfetchrow(row = pg_fetch_row(row=pgfetchrow(result)) {
echo "Author: row[0]E−mail:row[0] E-mail: row[0]Email:row[1]";
echo "
\n";
}?>`

See Also

Found A Problem?

pletiplot at seznam dot cz

18 years ago

Note, that when you retrieve some PG boolean value, you get 't' or 'f' characters which are not compatible with PHP bool.

eddie at eddiemonge dot com

15 years ago

pg_fetch_row is faster than pg_fetch_assoc when doing a query with * as the select parameter. Otherwise, with declared columns, the two are similar in speed.

darw75 at swbell dot net

23 years ago

`a way to do this with 2 loops to insert data into a table... num=pgnumrows(num = pg_numrows(num=pgnumrows(result); colnum=pgnumfields(col_num = pg_numfields(colnum=pgnumfields(result);

for ($i=0; i<i<i<num; $i++) {
line=pgfetcharray(line = pg_fetch_array(line=pgfetcharray(result, $i, PGSQL_ASSOC);
print "\t\n";
for ($j=0; j<j<j<col_num; $j++){
list($col_name, colvalue)=each(col_value) =each(colvalue)=each(line);
print "\t\t$col_value\n";
}
echo "
";
}

`

Matthew Wheeler

21 years ago

`Note that the internal row counter is incremented BEFORE the row is retrieved. This causes an off by one error if you try to do:

pg_result_seek($resid,0);
pg_fetch_row($resid);

you will get back the SECOND result not the FIRST.

`