For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > April 2007 > Re: [PHP-DB] querying results from postgresql









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] querying results from postgresql
Stut

2007-04-27, 6:57 pm

Kirk Wythers wrote:
> I am having trouble with a db query that was working not too long ago. I
> did upgrade from php4 to php5 and wonder if there are some differences
> that could explain my issue. Below is the code where I suspect things
> are breaking down.
>
>
> // build sql query from form values
> $select_query = "select * from grid_cell where township = $township
> and range = $range and rdir_shortname = '$rdir_shortname'
> and section = $section and forty_name = '$forty_name'";
> //echo $select_query; // use for debugging sql query
>
> // retrieve the query info from postgresql
> //this returns a resource, so put the resource into a variable
> $result = pg_query($select_query);
> //echo $result; // use for debugging sql query
>
> // process results. First argument pg_result is the returned result set
> ($grid_cell),
> // second argument is the row offset, and the third argument is the
> column offset.
> $forty_grid = pg_result($result, 0, 0);
> //echo $forty_grid; // use for debugging sql query
>
>
> If I uncomment echo $select_query, the scrip prints the proper query
> like this:
>
> select * from grid_cell where township = 67 and range = 1 and
> rdir_shortname = 'W' and section = 1 and forty_name = 'NWNW'
>
> However, if I uncomment echo $result, the script prints:
>
> Resource id #2
>
> and also the error:
>
> Unable to jump to row 0 on PostgreSQL result index 2
>
> I think that the variable $result is getting the result from pg_query
> and subsequently $forty_grid is not receiving what it expects either.
>
> Does anyone see what I'm not doing right here?


Just because the query returned a resource ID does not mean it contains
any results. An empty result set is what you're getting, and you need to
check for that before trying to use the results.

-Stut
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com