user2558534
user2558534

Reputation: 39

Select options from mysql query

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

Answers (2)

Emilio Gort
Emilio Gort

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

Kickstart
Kickstart

Reputation: 21533

Change

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

to

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

Upvotes: 2

Related Questions