简体   繁体   中英

Data not coming in dropdown box from MySQL

I am trying to get the data from MySQL table to 2 separate dropdown box, but Only first dropdown is getting the list of cities from database, why 2nd dropdown not showing the list of cities. Here is the code in PHP

<?php mysql_connect("localhost", "root", "") or die("Connection Failed");
mysql_select_db("flywest")or die("Connection Failed");
$query = "SELECT * FROM cities";
$result = mysql_query($query);
?>
Depart
<select name="formDepart" id="fromDepart">
  <?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?> 
  <option value="<?php echo $line['city_name'];?>"> <?php echo $line['city_name'];?> 
  </option> 
  <?php } ?>
</select>
</p>
Arrive
<select name="formArrive" id="fromDepart">
  <?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?> 
  <option value="<?php echo $line['city_name'];?>"> <?php echo $line['city_name'];?> 
  </option> 
  <?php } ?>
</select>  
<p> 

Kindly show my mistake.

Your 1st while loop has reached the end of result set and hence running the while again is starting from the end and could not go further.

You can fetch all the rows and store in a variable and then use that variable to populate both the dropdowns.

<?php     
$cities = array();
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $cities[] = $line;
}
?>

<select name="formDepart" id="fromDepart">
  <?php foreach ($cities as $line) { ?> 
  <option value="<?php echo $line['city_name'];?>"> <?php echo $line['city_name'];?> 
  </option> 
  <?php } ?>
</select>
</p>
Arrive
<select name="formArrive" id="fromDepart">
  <?php foreach ($cities as $line) { ?> 
  <option value="<?php echo $line['city_name'];?>"> <?php echo $line['city_name'];?> 
  </option> 
  <?php } ?>
</select>

This should work fine.

The problem is simple, your first while loop consumes all the results of the query, therefore the second while loop has nothing left to use.

So basically the second while loop has no data left to process.

One solution would be to reset the resultset pointer to the first entry in the result set before running the second while loop. So try this

<?php 
  mysql_connect("localhost", "root", "") or die("Connection Failed");
  mysql_select_db("flywest")or die("Connection Failed");

  $query = "SELECT * FROM cities";
  $result = mysql_query($query);
?>
Depart
<select name="formDepart" id="fromDepart">
<?php 
  while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 
?> 
    <option value="<?php echo $line['city_name'];?>"> <?php echo $line['city_name'];?> 
</option> 
<?php 
} 
?>
</select>
</p>
Arrive
<select name="formArrive" id="fromDepart">
<?php 

  // <-- new line to reset the resultset pointer
  mysql_data_seek($result,0);
  // >-- new line


  while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?> 
    <option value="<?php echo $line['city_name'];?>"> <?php echo $line['city_name'];?> 
    </option> 
<?php 
} 
?>
</select>  
<p> 

Obviously I should also point out the the MYSQL_* PHP extension is now deprecate and therefore should not be used. If you are writing new code please look at the MYSQLI_* extension or the PDO extention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM