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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:16:29+00:00 2026-06-04T02:16:29+00:00

I am trying to add 3 combo boxes which all display the exact same

  • 0

I am trying to add 3 combo boxes which all display the exact same information that comes from my MySQL db. It seems like the code I wrote makes the entire page wait until all 3 combo boxes are populated, before continuing.

<?
$query = "Select * from tblWriters order by surname";

for ($i = 1; $i <= 3; $i++) {

    $result = mysql_query($query);

    echo "<tr><td>Writer".$i." *</td><td>";
    echo "<select name='txtWriter".$i."' style='width: 200px;'>";
    echo "<option value ='' selected='selected'></option>";

    while ($row = mysql_fetch_array($result))
    {
         echo "<option value ='" . $row['id'] . "'> " . $row['surname'] . ", " . $row['name'] . "</option>";
    }

    echo "</select><td></tr>";
}
?>

I would like to optimize this piece of code, so the query will not be executed 3 times, as I believe this is where the page slows down.

If I put

$result = mysql_query($query);

outside of the for loop, the 2nd and 3rd combo box do not populate. I tried looking into resetting the pointer of the result, but I can’t seem to figure out how that works.

Also, is there a way I can reuse the while loop, so I don’t have to execute it 3 times?

Can someone point me in the right direction?
I’m pretty new to PHP and trying to learn on my own. Any help would be much appreciated. Thanks!

  • 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-04T02:16:32+00:00Added an answer on June 4, 2026 at 2:16 am

    If you move your mysql_query() out of the loop again, you can reset your mysql-result-pointer by using mysql_data_seek() at the beginning or end of your loop.

    This will result in:

     mysql_query($query);
     for($i=1;$i<=3;$i++);
     {
         mysql_data_seek(0); // reset datapointer
    
         // output querydata
     }
    

    I’m obliged however to point out that the mysql-extension is deprecated by now and you should use mysqli or pdo for new projects and code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set multiple combo boxes to all having the same set of
I am trying to populate combo boxes from a text file using comma as
I'm trying to display some elements from my XML file to my form which
I am trying to get all the data from a combo-box in vb. Lets
I am trying to set up a combo box that will display a UserName,
I'm trying add a tab to my web page that looks like this: Using
Trying to add the ability to delete a Folder using FTP and all subfolders
Trying to add a new element to the DOM but I get all sorts
I was trying to create a custom combobox that inherited from ComboBox, but I've
I'm trying to design a table in HTML which can add a row when

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.