From: Frank Joerdens <frank@joerdens.de>
To: Adam Haberlach <adam@newsnipple.com>
Cc: Uro Gruber <uros@sir-mag.com>
Cc: pgsql-general@postgresql.org
Subject: Re: PHP and PostgreSQL
Date: Sat, 06 Jan 2001 17:12:27 +0100
Message-ID: <3A5743EB.CFE29713@joerdens.de> (raw)
References: <631076786.20010105231729@sir-mag.com>
<20010105220727.B4224@newsnipple.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-php@postgresql.org
Cc: frank@joerdens.de, adam@newsnipple.com, uros@sir-mag.com, pgsql-general@postgresql.org
Subject: Re: PHP and PostgreSQL
In-Reply-To: <3A5743EB.CFE29713@joerdens.de>
References: <631076786.20010105231729@sir-mag.com> <20010105220727.B4224@newsnipple.com> <3A5743EB.CFE29713@joerdens.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox