Reputation: 39
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?
Upvotes: 2
Views: 7891
Reputation: 3470
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
Upvotes: 1
Reputation: 21533
Change
echo "<OPTION VALUE='".$row[0]."'>".$row[0]."</OPTION>";
to
echo "<OPTION VALUE='".$result[0]."'>".$result]."</OPTION>";
Upvotes: 2