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 8173187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:03:13+00:00 2026-06-06T22:03:13+00:00

Can anyone help – this is driving me mad. I am calling a mysql

  • 0

Can anyone help – this is driving me mad.

I am calling a mysql table and wish to display the results (or check the results with an if statement) more than once. But in this loop (see below) it only calls the table and its rows as an array in the first instance (of $i=1). This code will build 20 divs called Box but only populate the first one with table data. Why?

I think that I should be using a foreach loop within the outer loop (I have got this to work in wordPress with that btw) but can’t figure out the syntax. Any help would be really appreciated.

$i=1;
while ($i <= 20)
{
    echo "<div class=\"Box ".$i."\">";
    echo $i;
    echo "<br>";
    while ($row = mysql_fetch_array($result))
    {
    echo $row['name'];
    }
    echo "</div>";
    $i++;

}
  • 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-06-06T22:03:16+00:00Added an answer on June 6, 2026 at 10:03 pm

    If I understand, you are attempting to use the same result resource multiple times in a loop.

    After looping over the result resource the first time, you need to rewind it back to its original position to be able to loop over it again. It can be rewound with mysql_data_seek($result, 0), but a better strategy is to load the whole thing into an array before your loops and then iterate the array in each loop:

    // Load everything into the array `$results` first.
    $results = array();
    while ($row = mysql_fetch_assoc($result)) {
      $results[] = $row['name'];
    }
    
    // Better an incremental for loop than a while with a counter
    // for a situation where you know the endpoint values of $i
    for ($i = 1; $i <= 20; $i++)
    {
        echo "<div class='Box $i'>";
        echo $i;
        echo "<br>";
        // Inside your HTML, use the $results array 
        // rather than the MySQL resource
        foreach ($results as $row)
        {
          echo $row['name'];
        }
        echo "</div>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help solve this problem, no matter what i try i cannot display
Can anyone help me figure this out? The below code works fine and gets
Can anyone help me to convert this to proper JavaScript? <script>var datePostForAll = &#39;<div
Can anyone help me on how i can achieve this style for android ?
Can anyone help me with a MySQL query I'm trying to put together. I
Can anyone help me with this nonsense problem? <?php if(!readfile($path)) { $error = error_get_last();
Can anyone help me out with this query i m trying to execute, static
Can anyone help me with this. I would like to detect device such as
Can anyone help me troubleshoot why this PSCX cmdlet 'Write-Zip' is so slow? It
Can anyone help me? I can't fix this error when building an ios app

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.