Re: PHP and PostgreSQL

From: GH <grasshacker(at)over-yonder(dot)net>
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 17:22:07
Message-ID: 20010106112207.A59573@over-yonder.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-php

On Fri, Jan 05, 2001 at 10:07:28PM -0800, some SMTP stream spewed forth:
> 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.
>
$nr=pg_numrows($qu);
for($i=0; $i < $nr; $i++)

This add another variable in exchange for saving an obscene amount of
time counting rows on every iteration.

The same situation occures when going through arrays, only with arrays
if an array item is removed, then the number is different and may affect
the row count and thus the loop. I doubt that such could happen with
a resultset, but it is safer and a buttload faster to pre-count the rows.

gh

>>>>> for($i=0; $i < pg_numrows($qu); $i++) {

> $data = pg_fetch_object($qu, $row);
> echo $data->username." (";
> echo $data->password ." )";
> echo $data->client_id."<BR>>";
> }
>
> You can blame the PHP people for having wildly dissimilar systems
> for iterating through result sets...
>
> > In mysql there was no need to count rows.
>
> Let's not start the MySQL vs. Postgres wars again... :)
>
> --
> Adam Haberlach |A cat spends her life conflicted between a
> adam(at)newsnipple(dot)com |deep, passionate, and profound desire for
> http://www.newsnipple.com |fish and an equally deep, passionate, and
> '88 EX500 |profound desire to avoid getting wet.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ian deSouza 2001-01-06 17:42:35 Re: DROP SEQUENCE ?
Previous Message Tom Lane 2001-01-06 16:59:57 Re: Sequence bug or feature?

Browse pgsql-php by date

  From Date Subject
Next Message GH 2001-01-06 17:52:51 Re: PHP and PostgreSQL
Previous Message Frank Joerdens 2001-01-06 16:12:27 Re: PHP and PostgreSQL