PHP 5.6.30 Released


(PHP 4 >= 4.2.0, PHP 5, PHP 7)



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

pg_fetch_result() 根据由 pg_query() 返回的 result 资源返回相应的值。row 为整型数。field 为字段名(字符串)或字段索引(整数)。rowfield 指明了查询结果中的哪个单元被返回。行编号从 0 开始。除了用字段名之外,还可以用不带引号的数字作为字段索引。字段索引从 0 开始。

PostgreSQL 有很多内置的类型,这里只直接支持基本类型。所有形式的 integerboolean 和 void 类型都被返回为 integer 值。所有形式的 float 和 real 类型都被返回为 float 值。Boolean 类型被返回为 "t" 或者 "f"。所有其它类型包括数组都被返回为字符串,该字符串的格式和默认的 PostgreSQL 风格一样,可以在 psql 程序中看到。

add a note add a note

User Contributed Notes 4 notes

14 years ago
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";
Alan U. Kennington
10 years ago
See bug #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\"");
12 years ago
Use can use pg_fetch_result when getting a value (like a smallint as in this example) returned by your stored procedure

= pg_connect("dbname=users user=me");

$userNameToCheckFor = "metal";

$result = pg_query($pgConnection, "SELECT howManyUsersHaveThisName('$userNameToCheckFor')");

$count = pg_fetch_result($result, 0, 'howManyUsersHaveThisName');

Alan U Kennington
10 years ago
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() []: 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.
To Top