PHP: pg_fetch_result - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

pg_fetch_row pg_fetch_object

Last updated: Sat, 27 Jan 2007

view this page in

# pg_fetch_result

(PHP 4 >= 4.2.0, PHP 5)

pg_fetch_result -- Returns values from a result resource

## Описание

string pg_fetch_result ( resource result, int row, mixed field )
string pg_fetch_result ( resource result, mixed field )

pg_fetch_result() returns the value of a particular row and field (column) in a PostgreSQL result resource.

## Список параметров

`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, next row is fetched.

`field`

  A string representing the name of the field (column) to fetch, otherwise and int representing the field number to fetch. Fields are numbered from 0 upwards.

## Возвращаемые значения

Boolean is returned as "t" or "f". All other types, including arrays are returned as strings formatted in the same default PostgreSQL manner that you would see in the psql program. Database `NULL` values are returned as `NULL`.

`FALSE` is returned if `row` exceeds the number of rows in the set, or on any other error.

## Примеры

Пример 1. pg_fetch_result() example

`<?php
$db = pg_connect("dbname=users user=me") || die();
$res = pg_query($db, "SELECT 1 UNION ALL SELECT 2");
$val = pg_fetch_result($res, 1, 0);
echo "First field in the second row is: ", $val, "\n";
?>`

Результат выполнения данного примера:

    First field in the second row is: 2

## Смотрите также

pg_query()
pg_fetch_array()

add a note User Contributed Notes
pg_fetch_result
Alan U. Kennington
22-Sep-2006 06:49
`See bug #33809 http://bugs.php.net/bug.php?id=33809
Whether this really is a bug or a feature is not clear.
However, it is probably best to always put your column names in extra quotes.
$res = pg_query(...);
$colname = pg_field_name($res, $j);
pg_fetch_result($res, $i, "\"$colname\"");`
Alan U Kennington
22-Sep-2006 05:16
`In order to use upper case in pg_fetch_result column names, it is apparently necessary to include explicit quotation marks.
Thus when I do this sort of thing:
$res = pg_query(...);
$ncols = pg_num_fields($res);
for ($j = 0; $j < $ncols; ++$j) {
$colname[$j] = pg_field_name($res, $j);
$name = htmlspecialchars($colname[$j]);
print("Column $j name = \"$name\"\n");
$value = htmlspecialchars(pg_fetch_result($res, 0, $colname[$j]));
print("Column \"{$colname[$j]}\" value = \"$value\"\n");
}
I get this sort of thing:
[....]
Warning: pg_fetch_result() [function.pg-fetch-result]: Bad column offset specified in /.../view.php on line 247
Column 8 name = "VEC index"
Column "VEC index" value = ""
But if I change the $value line to this:
$value = htmlspecialchars(pg_fetch_result($res, 0, "\"$colname[$j]\""));
I get this:
[...]
Column 8 name = "VEC index"
Column "VEC index" value[0] = "47"
In my opinion, pg_fetch_result(...) should use the quotes already. In other words, this may be a bug in the PHP postgres library. It does not seem to be a documented feature of pg_fetch_result() although the postgresql manual documents it under "SQL syntax", "Lexical structure".
PHP version 5.1.4.
psql version 8.1.4.`
Akbar
01-Dec-2004 08:01
`Use can use pg_fetch_result when getting a value (like a smallint as in this example) returned by your stored procedure
<?php
$pgConnection = pg_connect("dbname=users user=me");
$userNameToCheckFor = "metal";
$result = pg_query($pgConnection, "SELECT howManyUsersHaveThisName('$userNameToCheckFor')");
$count = pg_fetch_result($result, 0, 'howManyUsersHaveThisName');
?>`
newby_AT_nobletec_DOT_com
04-Sep-2002 08:12
`Comment on boolean fields:
If you retrieve a boolean value from the PostgreSQL database, be aware that the value returned will be either the character 't' or the character 'f', not an integer. So, the statement
if (pg_fetch_result($rsRecords,0,'blnTrueFalseField')) {
echo "TRUE";
} else {
echo "FALSE";
}
will echo "TRUE" in either case (True or False stored in the field). In order to work as expected, do this instead:
if (pg_fetch_result($rsRecords,0,'blnTrueFalseField') == 't') {
echo "TRUE";
} else {
echo "FALSE";
}`
add a note
pg_fetch_row pg_fetch_object

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST