dongqian6554 2013-08-23 19:14
浏览 101

使用PHP在查询结果中填充下拉列表

The code should open all the rows of gamename column of games table and put 1700 rows into drop down menu, but it only displays a blank dropdown with 1700 rows.

// Connect to server and select database.
mysql_connect("$host", "$username", "$password") or die(mysql_error());

mysql_select_db("$db_name") or die(mysql_error());
$i=0;
$result = mysql_query("SELECT gamename FROM games");
$storeArray = Array();
echo '<select name="game" style="width: 400px">';

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      $storeArray[] =  $row[i];  

     echo "<option>".$storeArray[i]."</option>";
     $i= $i+1;
}

?>
</select>
  • 写回答

4条回答 默认 最新

  • doujie7886 2013-08-23 19:22
    关注
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $storeArray[] =  $row[i];  
    
        echo "<option>".$storeArray[i]."</option>";
        $i= $i+1;
    }
    

    For one thing, you're using i and $i interchangeably here; this may or may not cause an issue. You're assigning the ith member of $row into $storeArray, and that's not going to work after the first row, as there's only one item in your SELECT.

    Why not just do:

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "<option>".$row['gamename']."</option>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?