注意:类 mysqli_result 的对象无法转换为 int


Notice: Object of class mysqli_result could not be converted to int

我正在尝试从数据库中填充一个下拉框。查询显示在保管箱中,但有一个我不太明白的错误。

这是错误:

注意:类 mysqli_result 的对象无法转换为 int

。这是代码:

          <?php 
                   require_once('index.php'); //connect with the database.
$sql = "SELECT * FROM buffet";
$result = mysqli_query($conn,$sql);
if ($result != 0) {
    echo '<label>buffet:';
    echo '<select name="buffet">';
    echo '<option value="">all</option>';
    $num_results = mysqli_num_rows($result);
    for ($i=0;$i<$num_results;$i++) {
        $row = mysqli_fetch_array($result);
        $name = $row['buffet_id'];
        $name2 = $row['buffet_name'];
        echo '<option value="' .$name. '">' .$name2. '</option>';
    }
    echo '</select>';
    echo '</label>';
}

mysqli_close($conn);
?>
哦,

nvm.发现了我的错误。使用这个:

if ($result->num_rows > 0)

取而代之的是:

if ($result != 0)