Skip site navigation (1) Skip section navigation (2)

Re: display query results

From: PJ <af(dot)gourmet(at)videotron(dot)ca>
To: Lynna Landstreet <lynna(at)spidersilk(dot)net>
Cc: pgsql-php(at)postgresql(dot)org
Subject: Re: display query results
Date: 2008-07-30 20:07:35
Message-ID: 4890CA07.2040405@videotron.ca (view raw or flat)
Thread:
Lists: pgsql-php
Lynna Landstreet wrote:
> PJ wrote:
>> I've been looking at the pg_fetch_* functions but do not understand 
>> one little bit.
>> Using your suggestion gives a blank screen but var_dump($myarray) 
>> gives "bool(false)"
>> And a var_dump($results) returns "resource(3) of type (pgsql result) 
>> while print_r($results) returns "Resource id#3"
>> I'm totally lost. :(
>> Perhaps my query isn't well formulated?
>> What I am trying to locate (or print out) is the string found the 
>> "description" column whre the row in the "name" column is Alcohol.
>> item_id  ||  glossary_id ||  name     || description
>> 2           ||        2          || Alcohol  || One of thetwo 
>> major.... blah...blah.. blah 
>
> You can't print the results of a query directly - the format they are 
> retrieved in is not just a regular PHP array. That's what the various 
> "fetch" options do - they pull the data out of the resultset into a 
> regular variable (single or array, depending on which one you use) so 
> that you can print it or otherwise treat it as a normal variable.
>
> pg_fetch_result fetches a single value (string, integer, whatever it 
> might be) and stores it in a variable. You have to tell it exactly 
> which row and column from the result set you want. If you're only 
> after one value, use this.
>
> pg_fetch_array and pg_fetch_assoc each fetch a whole row of data, as 
> an array. The different between them is that with the first one, the 
> array keys are just numbers, and with the second, it's an associative 
> array where the keys are the names of the columns in your database. If 
> you've only retrieved one row, but need more than one value from that 
> row, use one of these.
>
> pg_fetch_all fetches *all* the rows from your result set, as a 
> multidimensional array. If your query is likely to have retrieved 
> multiple rows, use this.
>
> Does that help?
Well, it does explain things a little. Unfortunately, I have tried about 
everything imaginable before posting except the right thing.
I can not visualize what it is that my query is returning. Here is what 
the code is:

<?php
        $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'";
         $results = pg_query($db, $query) or die("Error in query: $query.
         " . pg_last_error($db));
var_dump ($results);
         $val = pg_fetch_result($results, 1, 3);
            echo $val, "\n";
         pg_close($db);
        ?>

Whatever  I enter as values for pg_fetch_result, the screen output is :

resource(3) or type (pgsql result)
*Warning*: pg_fetch_result() [function.pg-fetch-result 
<http://biggie/k2/function.pg-fetch-result>]: Unable to jump to row 1 on 
PostgreSQL result index 3 in 
*/usr/local/www/apache22/data/k2/test1_db.php* on line *29* 

I don't understand what $resuts is returning - if it is an entire row, 
the one that the field is in that I am looking for, then why do I not 
get a printout of the text that is in that field? The row in the table 
is the second row and the field I am trying to retrieve is the 4th field.
Am I querying correctly? The table is "glossary_item", the row I want is 
the one that is unique in containing the word "Alcohol" in the column 
"name"

I changed:   $query = "SELECT * FROM glossary_item WHERE name= 'Alcohol'";
same result

Picture me tearing out my hair...

In response to

Responses

pgsql-php by date

Next:From: Andy ShellamDate: 2008-07-30 20:37:56
Subject: Re: display query results
Previous:From: Raymond O'DonnellDate: 2008-07-30 18:44:09
Subject: Re: display query results

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group