如何从数据库中获取值并在下拉列表中显示

时间:2016-06-15 02:45:47

标签: php jquery html mysql database

我已经尝试了很多方法,但价值没有显示在下拉列表中  这是我的代码。你能告诉我任何我错的事吗

<?php  
$result = mysqli_query($con,"SELECT * FROM project"); 

    if( mysqli_num_rows( $result )==0){
    echo "<tr><td>No Rows Returned</td></tr>";
  }else{
    $row = mysqli_fetch_assoc( $result );
      $pos = 0;
      echo "<select name=Pname >"; 
        while($pos <= count ($row)){ 
      echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
           $pos++;
          }
echo "</select>";?>

我写为.php文件。谢谢你的帮助。

3 个答案:

答案 0 :(得分:0)

试试这个:

$output = '';
if(mysqli_num_rows($result) == 0){
    // echo error;
} else {
    while($row = mysqli_fetch_assoc($result)){
        $project_no = $row['project_no'];
        $project_name = $row['project_name'];

        $output .= '<option value="' . $project_no . '">' . $project_name . '</option>";
    }
}

然后在HTML内部,在$output元素内打印<select>变量:

<select>
<?php
    print("$output");
?>
</select>

它应该打印您从数据库请求的每一行的所有选项。

希望这会有所帮助:)

答案 1 :(得分:0)

试试这个:

$result = mysqli_query($con,"SELECT * FROM project");

if( mysqli_num_rows( $result )==0){
    echo "<tr><td>No Rows Returned</td></tr>";
}else{
    echo "<select name=Pname >";
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
    }
    echo "</select>";
}

答案 2 :(得分:0)

这是我可以运行它的结果代码。我将此代码放在html

的表单代码中

$result = mysqli_query($con,"SELECT * FROM project"); ?> <?php $output = ''; if(mysqli_num_rows($result) == 0){ // echo error; } else { echo " <select name = Pname>"; while($row = mysqli_fetch_assoc($result)){ $project_no = $row['project_no']; $project_name = $row['project_name']; $output = "<option value=" . $project_no . "> ". $project_name ." </option>"; print("$output"); } echo " </select>"; } ?>

谢谢大家帮助我^^