0

it's me again. I am trying to make a dropdown menu that will request it's data from a MySQL database (rows: category_id(a_i), category) and display it in the dropdown menu. When new rows are added, the dropdown menu should expand to house them. This is the code I've created so far:

<select>
    <select name="submit" id="category">
        <option>Select category</option>
<?php
    $query=$db->prepare("SELECT * FROM categories");
    $query->execute();
    while($rows=mysql_fetch_array($query)){
?>
        <option value="<?php echo $rows['']; ?>"></option>
<?php
    }
?>
</select>

db variable:

<?php
$db =mysqli_connect('localhost','xxx','xxx','xxx') or die('error with connection');
?>

However this code displays this error that I'm unable to fix on my own: Call to undefined method mysqli::mysqli_query()

4

1 回答 1

0

我不知道您有兴趣在下拉列表中输入什么列名,所以我使用了FIELDNAME.

试试这个,看看它是否给你足够的开始来自己完成它。

<select name="submit" id="category">
    <option value="0">Select category</option>
<?php
    $result = mysqli_query($db, "SELECT * FROM categories");
    if ( $result ) {
        while( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {

            echo '<option value="' . $row['FIELDNAME'] . '">' . $row['FIELDNAME'] . '</option>';

        }
    } else {
        // query failed so output some error info
    }
?>
</select>
于 2013-08-26T19:49:30.120 回答