dongpa2000
2016-06-15 02:45
浏览 314

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

I already try many ways but the value didn't show in dropdown list
Here, this is my code. can you suggest me anything that i was wrong

<?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>";?>

And i write as .php file. Thanks for your help.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douxu5845 2016-06-15 02:56
    已采纳

    Try this out:

    $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>";
        }
    }
    

    Then inside of your HTML, print your $output variable inside of your <select> element:

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

    It should print all options for every row that you have requested from the database.

    Hope this helps :)

    点赞 评论
  • dongtan5558 2016-06-15 02:58

    Try this:

    $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>";
    }
    
    点赞 评论
  • dongyan6235 2016-06-15 03:51

    This is the result code that i can run it. I put this code in a form code of 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>"; } ?>

    Thank you every one for helping me ^^

    点赞 评论

相关推荐 更多相似问题