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

3
  • FYI, you shouldn't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which one is best for you. Commented Apr 17, 2017 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() to get a detailed error message from the database. Commented Apr 17, 2017 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. Commented Apr 17, 2017 at 12:54

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

11 Comments

check last section for testing if everything fine
i think you missed return $option. so please check now
function get_name(){ $con=mysql_connect("localhost","root","root"); mysql_select_db("php",$con); $option=array(); $query="select name from login"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { foreach ($row as $value) { $option [$value['id']] = $value['name']; } } return $option; } i used this code it returning the value but only first charecter not whole string like gaurav in db its returning g only
this is separate function
<?php function get_name() { $con = mysql_connect("localhost", "root", "root"); mysql_select_db("php", $con); $option = array(); $query = "select name,id from login"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $option [$row['id']] = $row['name']; } return $option; }
|
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;

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.