reusing resource results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jstratman33
Forum Newbie
Posts: 3
Joined: Sat Jan 15, 2005 11:27 pm

reusing resource results

Post 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.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post 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.
jstratman33
Forum Newbie
Posts: 3
Joined: Sat Jan 15, 2005 11:27 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_data_seek(), however.. caching the data into an array is preferrable, but may consume a large amount of memory.
jstratman33
Forum Newbie
Posts: 3
Joined: Sat Jan 15, 2005 11:27 pm

Post by jstratman33 »

Thank you very much.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

but memory is much faster than db calls...
Post Reply