hello,
how could i populate drop down list from resultset of a mysql query??

Recommended Answers

All 10 Replies

Assuming you use mysqli_ extension, here is a sample code that might help you.

<?php
    echo "<select>";
    $result = mysqli_query("SELECT * FROM table_name");
    while($row=mysqli_fetch_array($result)){
        echo "<option value='".$arr['option_number']."'>";
        echo  $row['option_name'];
        echo  "</option>";
     }
     echo "</select>";
?>
    <?php

        $link = mysqli_connect('localhost', 'root', 'admin','admin') or die("Could not connect database");
        $result=mysqli_query($link, "SELECT * FROM questions");
        $row = mysqli_fetch_assoc($result);
    ?>

        <label for="login-username">Secret Question:</label>
            <select name="sq" id="sq" style="width:250px; height:30px">

                <?php do { ?>
                        <option ><?php echo $row['question'];  ?></option>
                <?php } while ($row = mysqli_fetch_assoc($result)); ?>

            </select> 

that is good..
but i want chained dropdown list.

like the options shown in dropdownlist2 is dependable on options of dropdownlist1...

i have used this code but this is not working

<form name="f1" action="wel.php" method="post" target="_blank">
            <select id="dbase" name="dbase">
                <?php
                            $link = mysqli_connect('localhost', 'root', '','people') 
                                    or die("Could not connect database");
$result=mysqli_query($link, "show databases");
        while($row=mysqli_fetch_assoc($result))
                {
            echo "<option value='".$row['Database']."'>";
        }
            echo "</option>";

        ?>
        </select>

this is not showing options in dropdown list..

You forgot:
1)To use the </option> tag inside the while loop.
2) To give a name to the option.
Change the while part to the following one.

while($row=mysqli_fetch_assoc($result))
{
            echo "<option value='".$row['Database']."'>";
            echo $row['Database'];
            echo "</option>";
}
Member Avatar for diafol

If you wanted a chained dropdown list, why didn't you state that in your original post? Have a look in the "code snippets" section of this forum. I provided 2 articles on linked dropdowns - you may find them useful.

Member Avatar for diafol

Please keep the discussion on site. State clearly what you want and we may be able to help. Asking contributors to search threads on other forums isn't really the way to go.

i have supplied u the whole url.no search needed

Member Avatar for diafol

OK, well if you're happy with that, good luck. Many of us won't click links to external sites. Just a heads up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.