pg.ddx.io pgsql-php@postgresql.org mailing list archive
help / color / mirror / Atom feedpg_fetch_array()
12+ messages / 9 participants
[nested] [flat]
* pg_fetch_array()
@ 2002-12-18 20:38 Thorsten Haude <postgresql@thorstenhau.de>
0 siblings, 5 replies; 12+ messages in thread
From: Thorsten Haude @ 2002-12-18 20:38 UTC (permalink / raw)
To: pgsql-php
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: pg_fetch_array()
@ 2002-12-18 21:12 Harry Waddell <waddell@caravan.com>
parent: Thorsten Haude <postgresql@thorstenhau.de>
4 siblings, 1 reply; 12+ messages in thread
From: Harry Waddell @ 2002-12-18 21:12 UTC (permalink / raw)
To: Thorsten Haude <postgresql@thorstenhau.de>; +Cc: pgsql-php
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: pg_fetch_array()
@ 2002-12-18 21:36 joerg.niemann <joerg.niemann@cityweb.de>
0 siblings, 0 replies; 12+ messages in thread
From: joerg.niemann @ 2002-12-18 21:36 UTC (permalink / raw)
To: postgresql@thorstenhau.de; +Cc: pgsql-php
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: pg_fetch_array()
@ 2002-12-18 21:44 Philipp Ottlinger <ottlinger@computer-systeme.de>
parent: Thorsten Haude <postgresql@thorstenhau.de>
4 siblings, 0 replies; 12+ messages in thread
From: Philipp Ottlinger @ 2002-12-18 21:44 UTC (permalink / raw)
To: pgsql-php
-----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-----
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: pg_fetch_array()
@ 2002-12-18 21:56 scott.marlowe <scott.marlowe@ihs.com>
parent: Thorsten Haude <postgresql@thorstenhau.de>
4 siblings, 0 replies; 12+ messages in thread
From: scott.marlowe @ 2002-12-18 21:56 UTC (permalink / raw)
To: Thorsten Haude <postgresql@thorstenhau.de>; +Cc: pgsql-php
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.
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: pg_fetch_array()
@ 2002-12-18 22:03 Thorsten Haude <postgresql@thorstenhau.de>
parent: Harry Waddell <waddell@caravan.com>
0 siblings, 0 replies; 12+ messages in thread
From: Thorsten Haude @ 2002-12-18 22:03 UTC (permalink / raw)
To: pgsql-php
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: pg_fetch_array()
@ 2002-12-18 22:04 scott.marlowe <scott.marlowe@ihs.com>
parent: Thorsten Haude <postgresql@thorstenhau.de>
4 siblings, 0 replies; 12+ messages in thread
From: scott.marlowe @ 2002-12-18 22:04 UTC (permalink / raw)
To: Thorsten Haude <postgresql@thorstenhau.de>; +Cc: pgsql-php
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);
}
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: pg_fetch_array()
@ 2002-12-18 22:55 Thorsten Haude <postgresql@thorstenhau.de>
parent: Thorsten Haude <postgresql@thorstenhau.de>
4 siblings, 0 replies; 12+ messages in thread
From: Thorsten Haude @ 2002-12-18 22:55 UTC (permalink / raw)
To: pgsql-php
Hi,
thanks to everyone who answered!
Thorsten
--
Good intentions will always be pleaded for every assumption of authority.
- Daniel Webster
^ permalink raw reply [nested|flat] 12+ messages in thread
* pg_fetch_array
@ 2004-06-14 05:02 Thanks <Thanks@verymuch.com>
0 siblings, 1 reply; 12+ messages in thread
From: Thanks @ 2004-06-14 05:02 UTC (permalink / raw)
To: pgsql-php
Hello....
I would like to know the performance of pg_fetch_array. Cosider the code:
$query = "select * from foo";
$result = pg_query( $db, $query );
while ($row = pg_fetch_array($result))
{
$a = $row["a"];
$b = $row["b"];
$c = $row["c"];
$d = $row["d"];
}
Does php need to read database everytime when pg_fetch_array is executed in
the while loop or all the rows have been in the memory after pg_query?
If read database is needed, is there any method to copy all the things into
memory by using other command? (because I have a application which needs
large amount database update/retrieval and I wish the performance of the
overall applications run faster.)
or other method you would like to recommend in order to make the faster
response time?
Thank you in advance.
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: pg_fetch_array
@ 2004-06-15 00:04 ljb <ljb220@mindspring.com>
parent: Thanks <Thanks@verymuch.com>
0 siblings, 1 reply; 12+ messages in thread
From: ljb @ 2004-06-15 00:04 UTC (permalink / raw)
To: pgsql-php
Thanks@verymuch.com wrote:
> Hello....
>
> I would like to know the performance of pg_fetch_array. Cosider the code:
>
> $query = "select * from foo";
> $result = pg_query( $db, $query );
>
> while ($row = pg_fetch_array($result))
> {
> $a = $row["a"];
> $b = $row["b"];
> $c = $row["c"];
> $d = $row["d"];
> }
>
> Does php need to read database everytime when pg_fetch_array is executed in
> the while loop or all the rows have been in the memory after pg_query?
The whole result query is stored in memory when pg_query returns, and
pg_fetch_array just gets the next row.
> If read database is needed, is there any method to copy all the things into
> memory by using other command? (because I have a application which needs
> large amount database update/retrieval and I wish the performance of the
> overall applications run faster.)
>
> or other method you would like to recommend in order to make the faster
> response time?
Actually, bringing the whole result into memory before processing any rows
can be slower, since the web server has to wait for the database server to
finish before it can start working on the data. So you can't get any
overlap of database and web server processing. As far as I know, there
isn't any alternative currently in the PHP PostgreSQL interface; there are
asynchronous queries but you still can't process any rows until they all
come back.
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: pg_fetch_array
@ 2004-06-15 04:51 Scott Marlowe <smarlowe@qwest.net>
parent: ljb <ljb220@mindspring.com>
0 siblings, 0 replies; 12+ messages in thread
From: Scott Marlowe @ 2004-06-15 04:51 UTC (permalink / raw)
To: ljb <ljb220@mindspring.com>; +Cc: pgsql-php
On Mon, 2004-06-14 at 18:04, ljb wrote:
> Thanks@verymuch.com wrote:
> > Hello....
> >
> > I would like to know the performance of pg_fetch_array. Cosider the code:
> >
> > $query = "select * from foo";
> > $result = pg_query( $db, $query );
> >
> > while ($row = pg_fetch_array($result))
> > {
> > $a = $row["a"];
> > $b = $row["b"];
> > $c = $row["c"];
> > $d = $row["d"];
> > }
> >
> > Does php need to read database everytime when pg_fetch_array is executed in
> > the while loop or all the rows have been in the memory after pg_query?
>
> The whole result query is stored in memory when pg_query returns, and
> pg_fetch_array just gets the next row.
>
> > If read database is needed, is there any method to copy all the things into
> > memory by using other command? (because I have a application which needs
> > large amount database update/retrieval and I wish the performance of the
> > overall applications run faster.)
> >
> > or other method you would like to recommend in order to make the faster
> > response time?
>
> Actually, bringing the whole result into memory before processing any rows
> can be slower, since the web server has to wait for the database server to
> finish before it can start working on the data. So you can't get any
> overlap of database and web server processing. As far as I know, there
> isn't any alternative currently in the PHP PostgreSQL interface; there are
> asynchronous queries but you still can't process any rows until they all
> come back.
Yes, operating on the whole set in memory at one time is not the best
job for PHP, but maybe for PostgreSQL.
And there is a way to retrieve some small percentage at a time, use a
cursor. See declare and fetch in the manual. Works a charm in PHP.
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: pg_fetch_array
@ 2004-06-21 21:09 George Essig <george_essig@yahoo.com>
0 siblings, 0 replies; 12+ messages in thread
From: George Essig @ 2004-06-21 21:09 UTC (permalink / raw)
To: Thanks@verymuch.com; +Cc: pgsql-php
Thanks@verymuch.com wrote:
> Does php need to read database everytime when pg_fetch_array is executed in
> the while loop or all the rows have been in the memory after pg_query?
The latter - pg_query puts all of the rows in memory. I found this out myself recently. Select a
lot of rows using pg_query without pg_fetch_array. As you're doing this, watch he memory usage
with 'free -s 1'.
George Essig
^ permalink raw reply [nested|flat] 12+ messages in thread
end of thread, other threads:[~2004-06-21 21:09 UTC | newest]
Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-12-18 20:38 pg_fetch_array() Thorsten Haude <postgresql@thorstenhau.de>
2002-12-18 21:12 ` Harry Waddell <waddell@caravan.com>
2002-12-18 22:03 ` Thorsten Haude <postgresql@thorstenhau.de>
2002-12-18 21:44 ` Philipp Ottlinger <ottlinger@computer-systeme.de>
2002-12-18 21:56 ` scott.marlowe <scott.marlowe@ihs.com>
2002-12-18 22:04 ` scott.marlowe <scott.marlowe@ihs.com>
2002-12-18 22:55 ` Thorsten Haude <postgresql@thorstenhau.de>
2002-12-18 21:36 Re: pg_fetch_array() joerg.niemann <joerg.niemann@cityweb.de>
2004-06-14 05:02 pg_fetch_array Thanks <Thanks@verymuch.com>
2004-06-15 00:04 ` Re: pg_fetch_array ljb <ljb220@mindspring.com>
2004-06-15 04:51 ` Re: pg_fetch_array Scott Marlowe <smarlowe@qwest.net>
2004-06-21 21:09 Re: pg_fetch_array George Essig <george_essig@yahoo.com>
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