mysqli calling same data twice [duplicate]

2019-09-21 11:21发布

This question already has an answer here:

Can anyone explain a detailed answer as, am not able to found it on stackoverflow

<?php
  error_reporting(E_ALL);
  ini_set('display_errors', 1);
  ini_set('log_errors',1);
  mysqli_report(MYSQLI_REPORT_ALL);
   $link = mysqli_connect('localhost', 'root', '', 'test') or 
  die(mysqli_connect_error());
    $query2 = "Select * from qualifications";
    $result=mysqli_query($link,$query2)or die (mysqli_error($link));   
    ?>
 <form>
  <table>
     <tr>
        <td>
           <select name="short_term_degree" id="short_term_degree" >
              <option value="">Select</option>
              <?php
                 while ( $d=mysqli_fetch_assoc($result)) {
                  echo "<option 
  value='".$d['qual_id']."'>".$d['qualification']."</option>";
                  }
                  ?>
           </select>
        </td>
     </tr>
     <tr>
        <td>
           <select name="short_term_course" id="short_term_course" >
              <option value="">Select</option>
              <?php
                 while ( $d=mysqli_fetch_assoc($result)) {
                 echo "<option 
  value='".$d['qual_id']."'>".$d['qualification']."</option>";
                 }
                 ?>
           </select>
        </td>
       </tr>
    </table>
  </form>
</body>

Result I Got

<select name="short_term_degree" id="short_term_course">
 <option value="">Select</option>
 <option value="268">Graduate</option>
 <option value="269">Mtech</option>
 <option value="353">Bachelor of Econimics</option>
</select>
<select name="short_term_course" id="short_term_course">
 <option value="">Select</option>
</select>

The Second Select Box Doesn't show any data What's the reason.

why do i require to use again mysqli_query() before while OR mysqli_data_seek($result,0); for getting result in select box

标签: php mysqli
3条回答
ら.Afraid
2楼-- · 2019-09-21 11:33

You can use mysqli_fetch_assoc twice in the following manner:-

How to go through mysql result twice?

查看更多
放荡不羁爱自由
3楼-- · 2019-09-21 11:51

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

The internal data point is still at the end when you try to use your second while loop. You have done nothing to reset it.

You can move it back to the start with mysqli_data_seek($result, 0);

Update:

when first while loop run the internal data pointer move ahead and reach the last data pointer . so while your trying to get data from that using second while loop . so there is no more data because .it's already reach last data pointer .

查看更多
做个烂人
4楼-- · 2019-09-21 11:57

Don't write same code twice use like this

<?php
  error_reporting(E_ALL);
  ini_set('display_errors', 1);
  ini_set('log_errors',1);
  mysqli_report(MYSQLI_REPORT_ALL);
   $link = mysqli_connect('localhost', 'root', '', 'test') or 
  die(mysqli_connect_error());
    $query2 = "Select * from qualifications";
    $result=mysqli_query($link,$query2)or die (mysqli_error($link));  
    $options = "";
    while ( $d=mysqli_fetch_assoc($result)) {
                  $options .= "<option 
  value='".$d['qual_id']."'>".$d['qualification']."</option>";
                  } 
    ?>
 <form>
  <table>
     <tr>
        <td>
           <select name="short_term_degree" id="short_term_degree" >
              <option value="">Select</option>
              <?php
                 echo $options;
                  ?>
           </select>
        </td>
     </tr>
     <tr>
        <td>
           <select name="short_term_course" id="short_term_course" >
              <option value="">Select</option>
              <?php
                 echo $options;
                 ?>
           </select>
        </td>
       </tr>
    </table>
  </form>
</body>
查看更多
登录 后发表回答