Thread: pg_fetch_array()
Hi, I want to read an unknown number of rows from a select result. I try this: - - - Schnipp - - - $result = pg_exec($dbh, $statement); $row = 0; while ($item = pg_fetch_array($result, $row, PGSQL_ASSOC)) { doSomething($item); $row++; } - - - Schnapp - - - However, I get an error telling me that PHP is "Unable to jump to row [$nRows + 1] on PostgreSQL result index 3 in [$file] on line [$line] What do I miss? tia, Thorsten -- Denn ein Tyrann ist nicht, wenn die Masse nicht geduldig stillhält. - Kurt Tucholsky
On Wed, 18 Dec 2002 21:38:32 +0100 Thorsten Haude <postgresql@thorstenhau.de> wrote: > Hi, > > I want to read an unknown number of rows from a select result. I try > this: > - - - Schnipp - - - > $result = pg_exec($dbh, $statement); > $row = 0; > while ($item = pg_fetch_array($result, $row, PGSQL_ASSOC)) > { > doSomething($item); > $row++; > } > - - - Schnapp - - - > > However, I get an error telling me that PHP is "Unable to jump to row > [$nRows + 1] on PostgreSQL result index 3 in [$file] on line [$line] > > What do I miss? > your using a while loop to process each result, but the last one which you expect to return NULL/failure has to evaluate pg_fetch_array first with the non-existent row index. You may be able to prepend @ to pg_fetch_array to supress the warning [you should check that $result is not NULL also], but I'd recommend using pg_NumRows and a for-loop instead. -- Harry Waddell Caravan Electronic Publishing
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 try @pg_fetch .... instead - it will take away the message due to error message cancelling - furthermore the loop finishes, when there is an error ;-) Thorsten Haude wrote: | | However, I get an error telling me that PHP is "Unable to jump to row | [$nRows + 1] on PostgreSQL result index 3 in [$file] on line [$line] | | What do I miss? | | | tia, | Thorsten - -- Philipp Ottlinger cS Computer & Systeme GmbH Menckenstr. 29 12169 Berlin Tel. +49-30-79748317 Fax +49-30-7226748 E-Mail:ottlinger@computer-systeme.de -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE+AOwnQogH4WkR2CwRAnB5AJ9qEBhP8g0c61TVnvPJkiWqCrAq/QCgydgP oVxb/gCqpiqdGgawpreu+A0= =K+9g -----END PGP SIGNATURE-----
Thorsten Haude wrote: >Hi, > >I want to read an unknown number of rows from a select result. I try >this: >- - - Schnipp - - - >$result = pg_exec($dbh, $statement); >$row = 0; >while ($item = pg_fetch_array($result, $row, PGSQL_ASSOC)) >{ > doSomething($item); > $row++; >} >- - - Schnapp - - - > >However, I get an error telling me that PHP is "Unable to jump to row >[$nRows + 1] on PostgreSQL result index 3 in [$file] on line [$line] This is a warning. The warning occurence is the exit signal for the while statement. If inside the while statement php is unable to jump to row x it is the signal to exit the while statement. Use pg_num_rows and for to avoid this. for ($i=0; $i < pg_num_rows($result); $i++) Joerg -- --------------------------------------- http://www.cityweb.de
On Wed, 18 Dec 2002, Thorsten Haude wrote: > Hi, > > I want to read an unknown number of rows from a select result. I try > this: > - - - Schnipp - - - > $result = pg_exec($dbh, $statement); > $row = 0; > while ($item = pg_fetch_array($result, $row, PGSQL_ASSOC)) > { > doSomething($item); > $row++; > } > - - - Schnapp - - - Wrap your while loop in this test: if (pg_num_rows($result)>0){ } It may be you're not getting any rows back here.
Hi, * Harry Waddell <waddell@caravan.com> [2002-12-18 22:12]: >Thorsten Haude <postgresql@thorstenhau.de> wrote: >> - - - Schnipp - - - >> $result = pg_exec($dbh, $statement); >> $row = 0; >> while ($item = pg_fetch_array($result, $row, PGSQL_ASSOC)) >> { >> doSomething($item); >> $row++; >> } >> - - - Schnapp - - - >> >> What do I miss? A clue-by-four, it seems. >your using a while loop to process each result, but the last one which you >expect to return NULL/failure has to evaluate pg_fetch_array first with the >non-existent row index. Of course. The only excuse I have is that I misread the documentation in a really stupid way. (Not much of an excuse, is it?) >You may be able to prepend @ to pg_fetch_array to supress the warning >[you should check that $result is not NULL also], but I'd recommend >using pg_NumRows and a for-loop instead. Sure, I'll do that. I don't want to suppress anyone, much less warnings. Thanks for your patience. Thorsten -- Endorsing products is the American way of expressing individuality. - Calvin
On Wed, 18 Dec 2002, Thorsten Haude wrote: > Hi, > > I want to read an unknown number of rows from a select result. I try > this: > - - - Schnipp - - - > $result = pg_exec($dbh, $statement); > $row = 0; > while ($item = pg_fetch_array($result, $row, PGSQL_ASSOC)) > { > doSomething($item); > $row++; > } > - - - Schnapp - - - > > However, I get an error telling me that PHP is "Unable to jump to row > [$nRows + 1] on PostgreSQL result index 3 in [$file] on line [$line] Sorry for the previously not quite right response. Since you are passing in PGSQL_ASSOC you have to supply a row number, do it like this: $stop = pg_num_rows($result); for ($i=0;$i<$stop;$i++){ doSomething($item); }
Hi, thanks to everyone who answered! Thorsten -- Good intentions will always be pleaded for every assumption of authority. - Daniel Webster