Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6104427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:50:27+00:00 2026-05-23T13:50:27+00:00

I want to show the same data in a page where the data was

  • 0

I want to show the same data in a page where the data was fetched from MySQL multiple times.

First I want to get data from MySQL using mysql_fetch_assoc() in a while loop and then show it as a menu. The second time I want to show the same data as a sitemap in the footer.

I’m currently calling mysql_fetch_assoc() twice, as follows:

// This one is the Menu
echo '<ul class="menu">';
while( $data = mysql_fetch_assoc($query) ) { 
    echo '<li><a href="page.php?id='.$data['id'].'">'.$data['title'].'</a>';
}
echo '</ul>';

// other page contents here

// at the footer - the small sitemap
echo '<ul class="sitemap">';
while( $f_data = mysql_fetch_assoc($query) ) { 
    echo '<li><a href="page.php?id='.$f_data['id'].'">'.$f_data['title'].'</a>';
}
echo '</ul>';

I think the code above might be using more resources than needed by querying the database twice. Since I have the data in the page, to fetch the same data again is wasting memory and not good.

So my questions are:

  1. Does it send separate queries and fetch data from the database each time I use mysql_fetch_assoc()? Or is it only fetching data from the database once, and later it just loops through an existing array?

  2. What is the best solution in my case?

Simply – Am I doing this in the best way? Is there any better way to do this, by not wasting memory/resources/time? Since it’s the same data I’m showing twice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:50:27+00:00Added an answer on May 23, 2026 at 1:50 pm

    Your best solution would be to store your retrieved data in an array, which you can then use whenever you want without having to make more queries to your database, like so:

    // First we make the query and store the rows in an array
    $result = mysql_query($query);
    $data_array = array();
    while ($data = mysql_fetch_assoc($result)) {
        $data_array[] = $data;
    }
    
    // Then we can loop through the values in that array without making further queries
    echo '<ul class="menu">';
    foreach ($data_array as $data) {
        echo '<li><a href="page.php?id=', $data['id'], '">', $data['title'], '</a>';
    }
    echo '</ul>';
    
    // Another loop through our array
    echo '<ul class="sitemap">';
    foreach ($data_array as $f_data) {
        echo '<li><a href="page.php?id=', $f_data['id'], '">', $f_data['title'], '</a>';
    }
    echo '</ul>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to show one master and one detail data. master data from sale
I want to show some bold and some simple word within the same sentence.If
If i want to show userLocation on the map, and at the same time
I want to show first element that is hidden by jquery. my html code
i want to write common page for viewing data list or single object. In
Basically I want to load a HTML document and using controls such as multiple
I want to show a Dialog, I am using jquery ui with asp.net mvc
I need to show a Master/Child data in a page and I have used
I am using PHP 5 to create a query page for a MySQL database
hii I want show a pop up Dialog box when i click on to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.