ppppp ppppp - 1 year ago 55
MySQL Question

how to get value from database and show in dropdown list

I already try many ways but the value didn't show in dropdown list

Here, this is my code. can you suggest me anything that i was wrong

$result = mysqli_query($con,"SELECT * FROM project");

if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
$row = mysqli_fetch_assoc( $result );
$pos = 0;
echo "<select name=Pname >";
while($pos <= count ($row)){
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
echo "</select>";?>

And i write as .php file. Thanks for your help.

Answer Source

Try this out:

$output = '';
if(mysqli_num_rows($result) == 0){
    // echo error;
} else {
    while($row = mysqli_fetch_assoc($result)){
        $project_no = $row['project_no'];
        $project_name = $row['project_name'];

        $output .= '<option value="' . $project_no . '">' . $project_name . '</option>";

Then inside of your HTML, print your $output variable inside of your <select> element:


It should print all options for every row that you have requested from the database.

Hope this helps :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download