Select options from mysql query

北城余情 提交于 2019-12-11 00:35:27

问题


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?


回答1:


Change

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

to

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



回答2:


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



来源:https://stackoverflow.com/questions/17516384/select-options-from-mysql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!