doubang9906
doubang9906
2017-04-17 12:53

如何使用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 douwang6635 4年前

    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);
    
    点赞 评论 复制链接分享
  • doy51723 doy51723 4年前

    should replace this

    while($row=mysql_fetch_array($result)){
       $option [$row->id] = strtoupper($row->name);
            }
    

    $option;

    by this code

    while($row=mysql_fetch_array($result)){
        $option [$row['id']] = strtoupper($row['name']);
            }
    

    return $option;

    点赞 评论 复制链接分享