RE: [PHP] pg_fetch_array etc

From: Date: Fri, 15 Dec 2000 20:01:35 +0000
Subject: RE: [PHP] pg_fetch_array etc
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-30581@lists.php.net to get a copy of this message
On Sat, 16 Dec 2000, Dan Harrington wrote: > Some questions: > > > 1) How do you know that it loops forever? > > Because the web browser keeps loading more and more and more data until it > crashes the > browser, or I have to manually kill it. > > > 2) Why aren't you using $HTTP_GET_VARS? > > who knows :-) It doesn't matter a whole lot, I guess I just chose another > way. > > > 3) Why are your pg_freeresult() and pg_close() calls outside of the if? > > good question. > :-) > > Still...it keeps spitting out the data forever. uggh > > Thanks > Dan > Okay, I think I see your problem. Here's your original code: > <? > $query = getenv("QUERY_STRING"); > parse_str($query); > > if ( $query != "" ) > { > $Query = "select * from generator where "; > $Query .= "orientation LIKE'%$orientation%' and category"; > $Query .= " LIKE '%$category%' and keywords LIKE '%$keywords%' > "; > > $Connection = pg_connect("", "", "", "", > "aaron"); > $Result = pg_exec($Connection, $Query); > > while ($Row = pg_fetch_row($Result, $Row)) > { > print("$Row[0], $Row[1], $Row[2], $Row[3]<br>\n"); > } > > } > pg_freeresult($Result); > pg_close($Connection); > ?> If you look at the documentation for pg_fetch_row() you'll notice that the third argument is an integer referring to the row number. AFAIK, arrays become 0 when interpreted as an integer, hence the infinite loop. Here's what your code should probably look more like (in my own style, feel free to change it): <? if (isset($HTTP_GET_VARS["orientation"]) and isset($HTTP_GET_VARS["category"]) and isset($HTTP_GET_VARS["keywords"])) { $query="SELECT * FROM generator WHERE"; $query.=" orientation LIKE '%{$HTTP_GET_VARS["orientation"]}%'"; $query.=" AND category LIKE '%{$HTTP_GET_VARS["category"]}%'"; $query.=" AND keywords LIKE '{$HTTP_GET_VARS["keywords"]}'"; $cxn = pg_connect("", "", "", "", "aaron"); $result = pg_exec($cxn, $query); for ($i=0; $i<pg_numrows($result); ++$i) { $row=pg_fetch_row($result, $i); print("$row[0], $row[1], $row[2], $row[3]<br>\n"); }; pg_freeresult($result); pg_close($cxn); }; ?> -- Ignacio Vazquez-Abrams <ignacio@openservices.net>

« previous php.general (#30581) next »