如何使用php

时间:2017-04-17 12:53:23

标签: php mysql

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

帮助我这个函数没有从表中返回我的名字而且我无法在选择框中看到列表..并告诉我如何才能看到函数将返回如何获取函数返回值, 我可以提醒返回值或回显值

2 个答案:

答案 0 :(得分:1)

尝试这个但未经过测试

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

}

检查此测试

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 :(得分:0)

应该替换 此

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

$选项;

通过此代码

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

返回$ option;