/    Sign up×
Community /Pin to ProfileBookmark

trouble with pg_fetch_array

I recently indexed a table and started getting the error message

/***** debug code – Query
pgsql/query (35.13s, 10000r): SELECT *
FROM products ORDER BY dept_id, cat_id, subcat_id, prod_name LIMIT 10000 :
**
***/

Warning: Unable to jump to row 10000 on PostgreSQL result index 4 in /home/jsafro/library/database.lib on line 109

That error refers to:
for( $count_row = 0; $row = pg_fetch_array($result, $count_row, PGSQL_ASSOC); $count_row++ )
$data[] = $row;

=====================

I have tried variations on the 2nd argument for pg_fetch_array. For example (changes in bold):

SELECT * FROM products ORDER BY dept_id, cat_id, subcat_id, prod_name LIMIT [B]10[/B]

for( $count_row = 0; $row = pg_fetch_array($result, [B]NULL[/B], PGSQL_ASSOC); $count_row++ )
$data[] = $row;

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 1105 bytes) in /home/jsafro/library/database.lib on line 109

The only thing that seems to work is dropping the 2nd (and 3rd) argument entirely. There is no difference between using a for loop and a while statement.

First, that doubles my query return size bc I am getting both numerically and text indexed data. Anyone know a way around this?

Second, anyone understand what is actually going wrong? Why did I start experiencing errors when I added the index?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@crh3675Sep 07.2004 — Why it doesn't work? Becuase you never stop incrementing your loop. It just keeps going and going and going. Try this instead:

<i>
</i>while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC))
{
$data[]=$row;
}


OR

<i>
</i>for( $count_row = 0; $count_row&lt;pg_num_rows($result); $count_row++ ){
$data[]=pg_fetch_array($result, $count_row, PGSQL_ASSOC);
}
Copy linkTweet thisAlerts:
@jacen6678authorSep 07.2004 — I tried both solutions... pg_num_rows() is unavailable to me bc the server is running php 4.1.

Using a null value for the second argument generates the error:

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 12 bytes) in /home/jsafro/library/database.lib on line 111

Line 111 is the location of the loop with pg_fetch_array(). The query return itself is only 10 rows in this case so that is not the source of the memory error.

===============

Also, I was terminating the for loop:

for( $count_row = 0; $row = pg_fetch_array($result, $count_row, PGSQL_ASSOC); $count_row++ )

$data[] = $row;

The for loop should run until pg_fetch_array() returns false. ?
Copy linkTweet thisAlerts:
@crh3675Sep 07.2004 — Try this:
<i>
</i>

$count=0;
while($row=pg_fetch_array($result)){
$count++;
}

for( $count_row = 0; $count_row &lt; $count; $count_row++ ){
$data[]=pg_fetch_array($result, $count_row, PGSQL_ASSOC);
}
Copy linkTweet thisAlerts:
@jacen6678authorSep 08.2004 — Thanks! That does work rather nicely... it is a bit slower though... I dropped the $row= from

while($row=pg_fetch_array($result)){

$count++;

}

to avoid an unnecessary value assignment. Do you think it would be more efficient to use:

while( pg_fetch_row( $result, $count ) )

count++;
×

Success!

Help @jacen6678 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...