RE: [PHP-DB] a more eloquent PostgreSQL/PHP version of MySQL/PHP routine

From: Date: Sun, 03 Dec 2000 23:03:21 +0000
Subject: RE: [PHP-DB] a more eloquent PostgreSQL/PHP version of MySQL/PHP routine
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-4995@lists.php.net to get a copy of this message
Doug (et all), You are correct re: the if statement. Usage of the if statement would assume the inclusion of an error handler in the event that there was a failure to return results. Did not consider the syntax you submitted.. a little more compact while remaining legible. Thanks for the input. Dave -----Original Message----- From: Doug Semig [mailto:dougslist@semig.com] Sent: Sunday, December 03, 2000 5:27 PM To: php-db@lists.php.net Subject: Re: [PHP-DB] a more eloquent PostgreSQL/PHP version of MySQL/PHP routine How about something like this? $query = "select * from something"; $result = pg_exec($database,$query); $tbNumRows=pg_numrows($result); for ($tbCount = 0; $tbCount < $tbNumRows; $tbCount++) { $row = pg_fetch_array($result,$tbCount); # or could use fetch row # do whatever with your row/result } The only reason I can think of to nest the do/while within an if for your MySQL solution is so you can have an else on the if to do something if no rows were returned. (Read that sentence to your grandma and see if she thinks your still human!) If you don't have to do anything if no rows are returned, then this might be more efficient (please forgive me, though, if it is wrong...I haven't used MySQL in a long time and am only going on memory): $query = "select * from something"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { # do whatever with your row } Doug At 02:41 PM 12/3/00 -0500, Dave VanAuken wrote: ># the following has worked well with MySQL. ># assume $database contains previously designated connection. > > $query = "select * from something"; > $result = mysql_query($query); > if ($row = mysql_fetch_array($result)) { > do { > # do whatever with your row > } while ($row = mysql_fetch_array($result)); > } > > ># using the same syntax causes a number of errors with PostgreSQL. ># have had to resort to the following. ># assume $database contains previously designated connection. > > $query = "select * from something"; > $result = pg_exec($database,$query); > if(pg_numrows($result)>0) { > $tbNumRows=pg_numrows($result); > $tbCount=0; > while($tbCount<$tbNumRows) { > $row = pg_fetch_array($result,$tbCount); # or could use fetch row > # do whatever with your row/result > $tbCount++; > } > } > > ># would appreciate any insight into streamlining the PostgreSQL code a >little more > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net For additional commands, e-mail: php-db-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.db (#4995) next »