|
pg_fetch_assoc
Fetch a row as an associative array
(PHP 4 >= 4.3.0, PHP 5)
Example 1922. pg_fetch_assoc() example<?php Code Examples / Notes » pg_fetch_assocluke
Note: PostgreSQL boolean values set to TRUE are returned as the string "t" PostgreSQL boolean values set to FALSE are returned as the string "f" javier dot vilarroig
Is worth to know that when you query on multiple tables only the first row with each name is returned. That is, if you are joining to tables with a column called 'name' you will receive only one field called name in the array and it will correspond to the one on the first table. Is advisable to allways allias your columns in that stuation. 24-may-2006 08:59
If you request a row that does not exist, it just fails, rather than simply returning false.
ninja whorl thinkninja stop com
If you are moving between different versions of PHP, this might be handy: if (!function_exists('pg_fetch_assoc')) { function pg_fetch_assoc ($result) { return @pg_fetch_array($result, NULL, PGSQL_ASSOC); } } johniskew
Here is another way to iterate a resultset and display all columns in very little code... might be faster than a foreach <?php print '<table>'; while($row=pg_fetch_assoc($rs2)) print '<tr><td>'.join('</td><td>',$row2).'</td></tr>'; print '</table>'; ?> brenton strickler
At a glance, the syntax listed at the top of this page doesn't match the example. The PGSQL_ASSOC flag isn't necessary.
spam
An important thing to note (as of PHP 4.3.2): If you are used to using the "extended" comparision operators (=== and !==) to try to make your code easier to follow visually, this function will return NULL if the provided resource handle is invalid (as opposed to false). ie, $rs = @pg_query('SELECT * FROM fake_table'); while (false !== ($row = @pg_fetch_assoc($rs))) { print_r($row); } Obviously you should check to see if $rs === false before you start the while loop, but this example is used to illustrate a potential infinite loop problem if $rs IS false. petrus
$dbconn3 = pg_connect("host=127.0.0.1 port=5432 dbname=blah user=blah password=blah"); $result = pg_query($dbconn3, "SELECT * FROM Packages"); echo "<HTML><HEAD><TITLE>PostgreSQL Test Page</TITLE></HEAD><BODY>"; echo "<TABLE>"; $pkg = pg_fetch_assoc($result); foreach ($pkg as $value) { echo "<TR><TD>$value"; echo "</TR></TD>"; } echo "</TABLE> "; echo "This package's full filename is: {$pkg['name']}-{$pkg['version']}{$pkg['extension']}"; echo "</BODY></HTML>"; For generating tables, this works, and personally I prefer foreach() to while loops because there's no danger of accidentally causing an infinite loop...foreach only works for as long as it has something to work with, and then stops. I thought the echo down the bottom might come in handy, too...took me a bit to find that out. |
Change Languagepg_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 |