0

我正在尝试从数据库中检索数据并从中填充列表,我的代码中似乎没有错误,但列表仍然没有填充,我的代码

<?php 
 $hostname = "localhost";
 $username = "username";
 $password = "password";
 $dbase = "db";
 $link = @ mysql_connect($hostname, $username, $password);
 $db_selected = @ mysql_select_db($dbase);
 ?>


<?php 
    include("scripts/dbconnect.php"); 
    $query="select class from school";
    $result=mysql_query($query);
    $numrows=mysql_num_rows($result);
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
     echo '<option value="'.$row['class'].'">'.$row['class'].'</option>';
    }  
   ?> 

非常感谢任何帮助,谢谢

4

2 回答 2

1

您需要回显一个select元素。

<?php 
    include("scripts/dbconnect.php"); 
    $query="select class from school";
    $result=mysql_query($query);
    $numrows=mysql_num_rows($result);
    echo "<select>"
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
     echo '<option value="'.$row['class'].'">'.$row['class'].'</option>';
    }  
    echo "</select>"
   ?> 
于 2012-04-12T20:43:14.833 回答
1

选项必须用 select 标签包裹:

 echo "<select name='class'>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
 echo '<option value="'.$row['class'].'">'.$row['class'].'</option>';
}  
 echo "</select>";

另外做:

if (!$link) {
    die('Could not connect: ' . mysql_error());
    break;
}

查看mysql连接是否有错误。

于 2012-04-12T20:47:53.943 回答