doubang9906 2017-04-17 04:53
浏览 35
已采纳

如何使用php中的函数从db获取名称列表

  1. This is my code to get name list from the database but I am not able to get values where is the error can anyone pls help me
  2. // database connection where php is my database name
  3. <?php
  4. $con=mysql_connect("localhost","root","root");
  5. mysql_select_db("php",$con);
  6. ?>
  7. // html body for getting value in option
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <title></title>
  12. </head>
  13. <body>
  14. <form method="post">
  15. <select name="select" >
  16. <option>Select College From Here</option>
  17. <?php $getoptions = get_name(); ?>
  18. <?php foreach ($getoptions as $key => $value) { ?>
  19. <option><?= $value ?></option>
  20. <?php } ?>
  21. </select>
  22. <input type="submit" name="sub">
  23. </form>
  24. // function to get name list from the database is this query right or wong ??
  25. <?php
  26. function get_name(){
  27. $option=array();
  28. $query="select * From login ";
  29. $result=mysql_query($query);
  30. while($row=mysql_fetch_array($result)){
  31. $option [$row->id] = strtoupper($row->name);
  32. }
  33. $option;
  34. }
  35. ?>

Help me regarding this that function not returning me the name from the table and m not able to see the list on select box.. and tell me how can i see the what function is going to return how to get function return value, can i alert the return value or echo the value

展开全部

  • 写回答

2条回答 默认 最新

  • douwang6635 2017-04-17 04:58
    关注

    try this but not tested

       function get_name()
    {
    // your code
        while($row=mysql_fetch_array($result)){
            $id = $row['id'];
            $option [$id] = strtoupper($row['name']);
        }
        return $option;
    
    }
    

    check this for test

    while ($row = mysqli_fetch_assoc($result)) {
            $id = $row['id'];
            echo $id . "==" . $row['name'];
            echo "<br>";
    
            $option [$id] = strtoupper($row['name']);
        }
        $option;
        print_r($option);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部