无法从数据库填充列表

时间:2012-04-12 20:39:32

标签: php sql database

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

<?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>';
    }  
   ?> 

任何帮助非常感谢,谢谢

2 个答案:

答案 0 :(得分: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>"
   ?> 

答案 1 :(得分: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连接是否有任何错误。