Adam Haberlach wrote:
>
> On Fri, Jan 05, 2001 at 11:17:29PM +0100, Uro Gruber wrote:
> > Hi!
> >
> > I have some questions about coding in php with postgre.
> >
> > Here is my code
> >
> > $qu = pg_exec ($db_conn, "SELECT * FROM clients ORDER BY username");
> > $row = 0; // postgres needs a row counter other dbs might not
> > while ($data = @pg_fetch_object ($qu, $row)) {
> > echo $data->username." (";
> > echo $data->password ."): ";
> > echo $data->client_id."<BR>";
> > $row++;
> > }
> >
> > When i execute this i get 3 records (in DB is also 3 records), if i
> > delete @ before pg_fetch_object i get an error:
> >
> > "Unable to jump to row 3 on PostgreSQL result index 4"
> >
> > I understand what's wrong and i know why is that @.
> >
> > What i do want to know is, if there is something wrong with this
> > function or am i doing something wrong. I don't like that kind of
> > errors. How can i stop before the end.
>
> for($i=0; $i < pg_numrows($qu); $i++) {
As I understand the mechanism, a while loop, as in
while ($data = @pg_fetch_object ($qu, $row)) { . . .
would be faster than a for loop as above because with each iteration, PHP has to execute
pg_numrows($qu), which, depending on how it is implemented (I don't know that and don't
read C well enough to be able to take a peek at the source to figure it out), would
require going through the entire result set to count the rows. Even if this only happens
once at the first iteration (e.g. if PHP then caches the result), this could be a
significant, unnecessary, overhead - particularly if the result set is large. With the
while loop you simply avoid that. I don't see a problem with using the error as an exit
condition for the loop, except that by switching off error reporting with @ you switch off
_all_ errors, not only those that you know you'll get and which don't want to see, which
can make debugging more difficult (but if you're debugging, you just remove the @ and add
it again when you're done).
Regards, Frank