Select options from mysql query

2019-07-22 13:23发布

I want a select/ dropdown menu with its options coming from the database using mysql select query.

PROBLEM: the dropdown menu displays the correct number of items inside the database but is not showing those items, just a blank option. e.g: there are four items in the database the dropdown menu has four blank options in it.

<label for="category">Category</label><select name=cat><option value=""> --Select Category-- </option>
    <?php $sql= mysqli_query($con, "SELECT category FROM taxonomy");
        while($result = mysqli_fetch_array($sql)){
            echo "<OPTION VALUE='".$row[0]."'>".$row[0]."</OPTION>";
        }
    ?>
    </select>

Can someone please tell me what's wrong?

2条回答
贪生不怕死
2楼-- · 2019-07-22 13:52

Change

echo "<OPTION VALUE='".$row[0]."'>".$row[0]."</OPTION>";

to

echo "<OPTION VALUE='".$result[0]."'>".$result]."</OPTION>";
查看更多
Root(大扎)
3楼-- · 2019-07-22 14:07

in $result add this

 mysqli_fetch_array($sql, MYSQLI_BOTH)

and

$result[0] instead $row[0]

http://php.net/manual/en/mysqli-result.fetch-array.php

查看更多
登录 后发表回答