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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.