简体   繁体   English

数据不在MySQL的下拉框中

[英]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. 我正在尝试将数据从MySQL表获取到2个单独的下拉框,但是只有第一个下拉列表从数据库中获取城市列表,为什么第二个下拉列表没有显示城市列表。 Here is the code in PHP 这是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. 您的第一个while循环已到达结果集的末尾,因此再次运行while将从末尾开始,并且无法继续进行。

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. 问题很简单,您的第一个while循环消耗了查询的所有结果,因此第二个while循环无用。

So basically the second while loop has no data left to process. 因此,基本上第二个while循环没有数据可处理。

One solution would be to reset the resultset pointer to the first entry in the result set before running the second while loop. 一种解决方案是在运行第二个while循环之前,将结果集指针重置为结果集中的第一个条目。 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. 显然,我还应该指出MYSQL_ * PHP扩展名已被弃用 ,因此不应使用。 If you are writing new code please look at the MYSQLI_* extension or the PDO extention. 如果要编写新代码,请查看MYSQLI_ *扩展名或PDO扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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