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

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

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 

// database connection where php is my database name
<?php
$con=mysql_connect("localhost","root","root");
mysql_select_db("php",$con);
?>

// html body for getting value in option

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="post">
<select name="select" >
    <option>Select College From Here</option>
     <?php $getoptions = get_name(); ?>
    <?php foreach ($getoptions as $key => $value) { ?>
        <option><?= $value ?></option>
    <?php } ?>
</select>
<input type="submit" name="sub">

</form>

// function to get name list from the database is this query right or wong ??
<?php
function get_name(){
    $option=array();
    $query="select * From login ";
    $result=mysql_query($query);
    while($row=mysql_fetch_array($result)){
        $option [$row->id] = strtoupper($row->name);
            }
$option;
}
?>

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 12: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条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退