php 警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,给出布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22651381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:10:06  来源:igfitidea点击:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given

phpmysqli

提问by user2526699

I've looked over all the other questions pertaining to this 'warning' but none are the same as my issue. My issue seems to be quit simple though i just can't seem to figure it out. I've tried every solution i've seen on google and here but i'm not familiar enough with php/mysqli to resolve it. Thanks in advance!

我已经查看了与此“警告”有关的所有其他问题,但没有一个与我的问题相同。我的问题似乎很简单,但我似乎无法弄清楚。我已经尝试了我在谷歌和这里看到的所有解决方案,但我对 php/mysqli 不够熟悉,无法解决它。提前致谢!

<?php
$db = new mysqli('localhost', 'root', '', 'game');

if($db->connect_errno > 0){
    die('Unable to connect [' . $db->connect_errno . ']');
}

$query = mysqli_query($db, "SELECT * FROM `characters`");

$query_result = array();

while ($row = mysqli_fetch_assoc($query)) {
    $query_result[$row['id']] = array(

        'name' => $row['name'],
        'name' => $row['race'],
        'name' => $row['level']
    );
}

?>

回答by Professor

This happens usually when the mysqli_query hasnt run properly. Check whether $query is executed properly or does it contains anything or not. The problem may be with the connection or it may be with the query or it may be with the tables. Be sure $query contains the results.

这通常发生在 mysqli_query 没有正常运行时。检查 $query 是否正确执行或是否包含任何内容。问题可能与连接有关,也可能与查询有关,也可能与表有关。确保 $query 包含结果。

Try this

尝试这个

if (!$query) {
        echo 'MySQL Error: ' . mysqli_error();
        exit;
    }

Let us know what is the mysql error

让我们知道什么是mysql错误