Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : PostgreSQL Functions : pg_fetch_result

pg_fetch_result

Returns values from a result resource (PHP 4 >= 4.2.0, PHP 5)
string pg_fetch_result ( resource result, int row, mixed field )
string pg_fetch_result ( resource result, mixed field )

Example 1924. 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";
?>

The above example will output:

First field in the second row is: 2

Code Examples / Notes » pg_fetch_result

akbar

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');
?>


alan u. kennington

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

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.


newby_at_nobletec_dot_com

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";
    }


Change Language


Follow Navioo On Twitter
pg_affected_rows
pg_cancel_query
pg_client_encoding
pg_close
pg_connect
pg_connection_busy
pg_connection_reset
pg_connection_status
pg_convert
pg_copy_from
pg_copy_to
pg_dbname
pg_delete
pg_end_copy
pg_escape_bytea
pg_escape_string
pg_execute
pg_fetch_all_columns
pg_fetch_all
pg_fetch_array
pg_fetch_assoc
pg_fetch_object
pg_fetch_result
pg_fetch_row
pg_field_is_null
pg_field_name
pg_field_num
pg_field_prtlen
pg_field_size
pg_field_table
pg_field_type_oid
pg_field_type
pg_free_result
pg_get_notify
pg_get_pid
pg_get_result
pg_host
pg_insert
pg_last_error
pg_last_notice
pg_last_oid
pg_lo_close
pg_lo_create
pg_lo_export
pg_lo_import
pg_lo_open
pg_lo_read_all
pg_lo_read
pg_lo_seek
pg_lo_tell
pg_lo_unlink
pg_lo_write
pg_meta_data
pg_num_fields
pg_num_rows
pg_options
pg_parameter_status
pg_pconnect
pg_ping
pg_port
pg_prepare
pg_put_line
pg_query_params
pg_query
pg_result_error_field
pg_result_error
pg_result_seek
pg_result_status
pg_select
pg_send_execute
pg_send_prepare
pg_send_query_params
pg_send_query
pg_set_client_encoding
pg_set_error_verbosity
pg_trace
pg_transaction_status
pg_tty
pg_unescape_bytea
pg_untrace
pg_update
pg_version
eXTReMe Tracker