Page 1 of 1

reusing resource results

Posted: Sat Jan 15, 2005 11:34 pm
by jstratman33
I am a fairly new PHP programmer so forgive me if this is elementary.

I need to use an array with multiple results from a query more than once on the same page, but I can't seem to find out how to go back to the first result and display the result again. I've looked all over and I'm just stuck.

...

Posted: Sun Jan 16, 2005 2:04 am
by Calimero
If I understood corectly:

First construct an empty array.

When you query the database - you need to add each result to the array.

Then that array ( can be multi-dimensional for more data storage ) members can be called when ever you need them - to show their contents (numerous times ) on the page.


Is this what you meant.

Posted: Sun Jan 16, 2005 9:14 am
by jstratman33
OK, let's try this. Here's basically what I have set up.

Code: Select all

$query = "SELECT * FROM members WHERE state = 'NY' ORDER BY last_name";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);

for ($i=0; $i<$num_rows; $i++) &#123;
  $row = mysql_fetch_array($result);
  echo '<b>'.$row&#1111;'last_name'].', '.$row&#1111;'first_name'].'</b><br>';
&#125;
Now, when I try to list the results again in another FOR loop, all I get is a NULL value. How do I get back to the first row of the result?

Posted: Sun Jan 16, 2005 9:31 am
by feyd
mysql_data_seek(), however.. caching the data into an array is preferrable, but may consume a large amount of memory.

Posted: Sun Jan 16, 2005 10:17 am
by jstratman33
Thank you very much.

Posted: Sun Jan 16, 2005 10:49 am
by magicrobotmonkey
but memory is much faster than db calls...