Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 32E4A6504E0 for ; Wed, 30 Jul 2008 19:02:24 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 83478-05-4 for ; Wed, 30 Jul 2008 19:02:16 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from nathalia.gransy.com (nathalia.gransy.com [82.208.29.194]) by postgresql.org (Postfix) with ESMTP id 1A4BE650499 for ; Wed, 30 Jul 2008 19:02:13 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by nathalia.gransy.com (Postfix) with ESMTP id 33B727AC0D0; Wed, 30 Jul 2008 23:39:08 +0200 (CEST) Received: from nathalia.gransy.com ([127.0.0.1]) by localhost (nathalia.gransy.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id sCUzh88bVMIX; Wed, 30 Jul 2008 23:39:08 +0200 (CEST) Received: from [192.168.1.1] (ip-89-102-139-23.karneval.cz [89.102.139.23]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by nathalia.gransy.com (Postfix) with ESMTP id EBD997AC0AD; Wed, 30 Jul 2008 23:39:07 +0200 (CEST) Message-ID: <4890DD72.2020400@fuzzy.cz> Date: Wed, 30 Jul 2008 23:30:26 +0200 From: Tomas Vondra User-Agent: Thunderbird 2.0.0.9 (X11/20080213) MIME-Version: 1.0 To: PJ CC: Matthias Ritzkowski , pgsql-php@postgresql.org Subject: Re: display query results References: <48909454.7040305@videotron.ca> <4890A983.2040005@videotron.ca> In-Reply-To: <4890A983.2040005@videotron.ca> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200807/15 X-Sequence-Number: 3879 See, you're working with a database - you execute a query but the data (result) is still in the database. The '$result' variable represents just a link to the data in the database - that's why it prints 'Resource id #3'. That's almost the same as the '$connection' variable - I guess you don't expect this to print all the data in the database: So you have to fetch the data from resultset from the database process into PHP, and that's what pg_fetch_* functions are for. Each time you call pg_fetch_array($result) it fetches and returns the next row (or FALSE if there are no more rows). The rows returned by pg_fetch_array are associative arrays, keys being the field names. So something like this should work: If it prints nothing, then the resultset is empty - try to run the same query pro psql or some other SQL interface. Tomas > Doesn't work... and I feel totally lost. I don't understand the > pg_fetch_* stuff at all. > One would think that my query should return the text that is in the > field. The database is quite correct and does work with php since it > comes from an older web-site that someone else programmed for me. I'm > using it to practice and learn. :) > > Matthias Ritzkowski wrote: >> Try this: >> >> $db = pg_connect("host=localhost port=5432 dbname=med user=med >> password=0tscc71"); >> >> if (!$db) >> { >> die("Could not open connection to database server"); >> } >> >> // generate and execute a query >> $query = "SELECT description FROM glossary_item WHERE >> name='Alcohol'"; >> $result = pg_query($db, $query) or die("Error in query: $query. >> " . pg_last_error($db)); >> >> // Print result on screen >> while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) { >> foreach ($line as $col_value) { >> echo $col_value."
"; >> } >> >> pg_close($db); >> >> >> The result set is an array, You just need to loop through it. The php >> manual has some nice examples that helped me get started. >> -------------------------------- >> >> >> Matthias Ritzkowski >> >> > >