Jump to content

Reuse mysql_fetch_assoc data


gschimek

Recommended Posts

Can I use the data fetched with the mysql_fetch_assoc command more than once?  What I'm trying to do is query a MySQL DB, and print a list of the results by gender.  So far I've only been able to do it by querying the DB twice, and I know there's got to be a more efficient way.  I think I should be able to go through the array once, print all the males, then go through the array again and print all the females.  But I'm stumped as to how to do it.

 

Here's basically what I'm doing to get my results:

 

$cxn=mysqli_connect("localhost","username","password","table_name") or die ("Could not connect to database");               
$query= "SELECT * FROM blah blah blah";
$result=mysqli_query($cxn,$query) or die ("Couldn't execute query.");
                            
while ( $row = mysqli_fetch_assoc($result)) 
{
           extract($row);
           IF ($gender == 'M')
               {
                  echo "Here is my data for Males";
                }
}

 

I basically want to re-run the WHILE statement, but check for $gender == 'F', without having to re-run the $result=mysqli_query command again.  But when I do that, nothing is displayed and the script ends. Is it just a matter of moving a pointer to the beginning of an array or something similar?

 

Thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/245647-reuse-mysql_fetch_assoc-data/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.