Received: from rakete.joerdens.de (rakete.joerdens.de [62.104.116.200]) by mail.postgresql.org (8.11.1/8.11.1) with ESMTP id f06GCJ785288 for ; Sat, 6 Jan 2001 11:12:19 -0500 (EST) (envelope-from frank@joerdens.de) Received: from limedes.archi-me-des.de (pD9002565.dip.t-dialin.net [217.0.37.101]) by rakete.joerdens.de (8.9.3/8.9.3) with ESMTP id RAA01413; Sat, 6 Jan 2001 17:35:33 +0100 Received: from joerdens.de (tricky.archi-me-des.de [192.168.100.1]) by limedes.archi-me-des.de (8.9.3/8.9.3) with ESMTP id QAA31916; Sat, 6 Jan 2001 16:56:11 +0100 Message-ID: <3A5743EB.CFE29713@joerdens.de> Date: Sat, 06 Jan 2001 17:12:27 +0100 From: Frank Joerdens X-Mailer: Mozilla 4.72 [en] (WinNT; I) X-Accept-Language: en MIME-Version: 1.0 To: Adam Haberlach CC: Uro Gruber , pgsql-general@postgresql.org Subject: Re: PHP and PostgreSQL References: <631076786.20010105231729@sir-mag.com> <20010105220727.B4224@newsnipple.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Archive-Number: 200101/223 X-Sequence-Number: 2392 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."
"; > > $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