Re: PHP and PostgreSQL

From: Frank Joerdens <frank(at)joerdens(dot)de>
To: Adam Haberlach <adam(at)newsnipple(dot)com>
Cc: Uro Gruber <uros(at)sir-mag(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: PHP and PostgreSQL
Date: 2001-01-06 16:12:27
Message-ID: 3A5743EB.CFE29713@joerdens.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-php

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ian deSouza 2001-01-06 16:54:19 Re: DROP SEQUENCE ?
Previous Message rob 2001-01-06 14:15:03 Sequence bug or feature?

Browse pgsql-php by date

  From Date Subject
Next Message GH 2001-01-06 17:22:07 Re: PHP and PostgreSQL
Previous Message Partyka Robert 2001-01-06 09:14:37 Re: PHP and PostgreSQL