Discussion:
resetting pg_fetch_array
Dave O
2005-02-22 16:01:34 UTC
Permalink
Hi all. I'm been trying the following code for the past day or so and I
can't seem to get around it. I'm issuing a query to pgsql and
iterating through the results twice in order to facilitate the
separation of shipment history.

Below is the simplest example I can come up with. The results only
display once, even though I'm not issuing any other db calls after the
1st iteration and the resouce id remains the same. Heck, even
pg_num_rows returns the same result twice, but the results just won't
display a second time.

I'm running this on freebsd 5.3 using pgsql v8 and php 4.3.10. Anybody
got any suggestions. TIA.

Dave

table looptest
rownum | rowstr
--------+--------
1 | aaaaaa
2 | bbbbbb
3 | cccccc
4 | dddddd

include ('connection.inc.php');
$sql = "SELECT rownum, rowstr FROM looptest";
$result = pg_query($conn, $sql);
$c=2;
$x=0;
echo "<pre>\n";
while ($x<$c)
{
echo pg_num_rows($result) . "->";
print_r($result);
echo "\n\n";
reset($result);
while($row = pg_fetch_array($result))
{
echo $row["rownum"] . " -> " . $row["rowstr"] . " \n";
}

echo "====================================\n";

$x++;

}

echo "</pre>\n";

----------------------------------
results
----------------------------------

4->Resource id #4

1 -> aaaaaa
2 -> bbbbbb
3 -> cccccc
4 -> dddddd
====================================
4->Resource id #4

====================================
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Richard Lynch
2005-02-22 17:29:24 UTC
Permalink
Post by Dave O
Hi all. I'm been trying the following code for the past day or so and I
can't seem to get around it. I'm issuing a query to pgsql and
iterating through the results twice in order to facilitate the
separation of shipment history.
Below is the simplest example I can come up with. The results only
display once, even though I'm not issuing any other db calls after the
1st iteration and the resouce id remains the same. Heck, even
pg_num_rows returns the same result twice, but the results just won't
display a second time.
I'm running this on freebsd 5.3 using pgsql v8 and php 4.3.10. Anybody
got any suggestions. TIA.
Dave
table looptest
rownum | rowstr
--------+--------
1 | aaaaaa
2 | bbbbbb
3 | cccccc
4 | dddddd
include ('connection.inc.php');
$sql = "SELECT rownum, rowstr FROM looptest";
$result = pg_query($conn, $sql);
$c=2;
$x=0;
echo "<pre>\n";
while ($x<$c)
{
echo pg_num_rows($result) . "->";
print_r($result);
echo "\n\n";
reset($result);
while($row = pg_fetch_array($result))
This automatically advances an internal "counter" to the next row.

You never "rewind" the counter.
[Be kind, rewind!]

You can either find the pg_data_seek function that does the rewind, or you
can do a counter yourself and pass in the optional row number argument to
pg_fetch_array:

$row_num = 0;
while ($row = pg_fetch_array($result, $row_num++))

The reset function you are using is for ARRAYS, and has nothing to do with
pg resources and database result sets.

I'm surprise you are not getting some kind of error on that...

Or maybe you aren't using 'error_reporting(E_ALL)' like you should.
Post by Dave O
{
echo $row["rownum"] . " -> " . $row["rowstr"] . " \n";
}
echo "====================================\n";
$x++;
}
echo "</pre>\n";
----------------------------------
results
----------------------------------
4->Resource id #4
1 -> aaaaaa
2 -> bbbbbb
3 -> cccccc
4 -> dddddd
====================================
4->Resource id #4
====================================
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Like Music?
http://l-i-e.com/artists.htm
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Dave O
2005-02-22 17:53:27 UTC
Permalink
Post by Richard Lynch
Post by Dave O
Hi all. I'm been trying the following code for the past day or so and I
can't seem to get around it. I'm issuing a query to pgsql and
iterating through the results twice in order to facilitate the
separation of shipment history.
Below is the simplest example I can come up with. The results only
display once, even though I'm not issuing any other db calls after the
1st iteration and the resouce id remains the same. Heck, even
pg_num_rows returns the same result twice, but the results just won't
display a second time.
I'm running this on freebsd 5.3 using pgsql v8 and php 4.3.10. Anybody
got any suggestions. TIA.
Dave
table looptest
rownum | rowstr
--------+--------
1 | aaaaaa
2 | bbbbbb
3 | cccccc
4 | dddddd
include ('connection.inc.php');
$sql = "SELECT rownum, rowstr FROM looptest";
$result = pg_query($conn, $sql);
$c=2;
$x=0;
echo "<pre>\n";
while ($x<$c)
{
echo pg_num_rows($result) . "->";
print_r($result);
echo "\n\n";
reset($result);
while($row = pg_fetch_array($result))
This automatically advances an internal "counter" to the next row.
You never "rewind" the counter.
[Be kind, rewind!]
You can either find the pg_data_seek function that does the rewind, or you
^ ^ ^ ^
pg_result_seek ???
Post by Richard Lynch
can do a counter yourself and pass in the optional row number argument to
$row_num = 0;
while ($row = pg_fetch_array($result, $row_num++))
The reset function you are using is for ARRAYS, and has nothing to do with
pg resources and database result sets.
I'm surprise you are not getting some kind of error on that...
Or maybe you aren't using 'error_reporting(E_ALL)' like you should.
Post by Dave O
stuff snipped
Yep, pg_result_seek did the trick. Using:

pg_result_seek($result, 0)

..just before the 'while' statement makes the results show up twice. I
reread the manual page for this and understood there was a counter, but
it didn't click that there might be a pg function to handle this. I
took the mention of "array" too seriously and was looking under the
array functions.

Btw, E_ALL is set, but following suggestions in these lists and the
site, I've it set to log to a file and not display any errors. That's
probably something to reconsider for development.

Thanks for the help Richard. Much appreciated.

Dave
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...