title: PHP: using SQL result in a loop without re-executing query
body: <p>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.</p>
  
  <pre><code>&lt;?
  $query = "Select * from tblWriters order by surname";
  
  for ($i = 1; $i &lt;= 3; $i++) {
  
      $result = mysql_query($query);
  
      echo "&lt;tr&gt;&lt;td&gt;Writer".$i." *&lt;/td&gt;&lt;td&gt;";
      echo "&lt;select name='txtWriter".$i."' style='width: 200px;'&gt;";
      echo "&lt;option value ='' selected='selected'&gt;&lt;/option&gt;";
  
      while ($row = mysql_fetch_array($result))
      {
           echo "&lt;option value ='" . $row['id'] . "'&gt; " . $row['surname'] . ", " . $row['name'] . "&lt;/option&gt;";
      }
  
      echo "&lt;/select&gt;&lt;td&gt;&lt;/tr&gt;";
  }
  ?&gt;
  </code></pre>
  
  <p>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. </p>
  
  <p>If I put </p>
  
  <blockquote>
    <p>$result = mysql_query($query);</p>
  </blockquote>
  
  <p>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. </p>
  
  <p>Also, is there a way I can reuse the while loop, so I don't have to execute it 3 times?</p>
  
  <p>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!</p>