Re: display query results

Raymond O'Donnell <[email protected]>
Newsgroups gmane.comp.db.postgresql.php
Message-ID <[email protected]>
On 30/07/2008 18:48, PJ wrote:
> Doesn't work... and I feel totally lost. I don't understand the 
> pg_fetch_* stuff at all.

Here's what you need to do:

// Open a connection to the database.
$conn = pg_connect($your_connection_string);

// Execute a query which returns some rows.
$result = pg_query($conn, 'select foo, bar from your_table');

// Loop through the rows one by one.
// pg_fetch_array() returns the row as an array with a numeric index,
// while pg_fetch_assoc() returns the row as an associative array
// keyed on the column names. I usually use the latter. Both return
// false when no more rows are available, so this is the usual idiom:
while ($row = pg_fetch_assoc($result))
{
   print 'Some values: ' . $row['foo'] . ', ' . $row['bar'] . "\n";
}

The other pg_fetch_* functions do various other things, such as (for 
example) fetching a value from a particular column in a particular row.

HTH,

Ray.



------------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
[email protected]
Galway Cathedral Recitals: http://www.galwaycathedral.org/recitals
------------------------------------------------------------------

-- 
Sent via pgsql-php mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.