0
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

  • FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Apr 17 '17 at 12:53
  • You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use [`mysql_error()`](http://php.net/manual/en/mysql.error.php) to get a detailed error message from the database. – John Conde Apr 17 '17 at 12:53
  • You're fetching data as an array and then trying to access it as an object. If you turned on error reporting PHP would have warned you about this. – John Conde Apr 17 '17 at 12:54

2 Answers2

1

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);
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
0

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;

  • Why should the OP "replace" this? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Apr 17 '17 at 13:58