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 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题