php 警告问题:期望参数 1 为 mysqli_result

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2077263/
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 04:54:47  来源:igfitidea点击:

warning problem: expects parameter 1 to be mysqli_result

phpmysqlerror-handlingmysqli

提问by tEcHnUt

I get the following warning listed below and I was wondering how do I fix it

我收到下面列出的以下警告,我想知道如何解决它

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given on line 65

The code is around this section of PHP code listed below. I can list the full code if needed.

该代码围绕下面列出的 PHP 代码部分。如果需要,我可以列出完整的代码。

// function to retrieve average and votes
function getRatingText(){
    $dbc = mysqli_connect ("localhost", "root", "", "sitename");
    $sql1 = "SELECT COUNT(*) 
             FROM articles_grades 
             WHERE users_articles_id = '$page'";

    $result = mysqli_query($dbc,$sql1);
    $total_ratings = mysqli_fetch_array($result);

    $sql2 = "SELECT COUNT(*) 
             FROM grades 
             JOIN grades ON grades.id = articles_grades.grade_id
             WHERE articles_grades.users_articles_id = '$page'";

    $result = mysqli_query($dbc,$sql2);
    $total_rating_points = mysqli_fetch_array($result);
    if (!empty($total_rating_points) && !empty($total_ratings)){
        $avg = (round($total_rating_points / $total_ratings,1));
        $votes = $total_ratings;
        echo $avg . "/10  (" . $votes . " votes cast)";
    } else {
        echo '(no votes cast)';
    }
}

回答by Michael Waterfall

mysqli_query()returns FALSEif there was an error in the query. So you should test for it...

mysqli_query()FALSE如果查询中有错误则返回。所以你应该测试一下...

/* Select queries return a resultset */
if ($result = mysqli_query($dbc, "SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", $result->num_rows);

    /* free result set */
    $result->close();
}

See this link for the mysqli_queryreference http://php.net/manual/en/mysqli.query.php

请参阅此链接以获取mysqli_query参考 http://php.net/manual/en/mysqli.query.php

回答by Salman A

Waterfall is probably right. Revise your code as follows:

瀑布可能是对的。修改您的代码如下:

$result = mysqli_query($dbc,$sql1) or die(mysqli_error($dbc));
// and
$result = mysqli_query($dbc,$sql2) or die(mysqli_error($dbc));

PS: Just wondering what exactly is $page? Did you forget to do a:

PS:只是想知道究竟是$page什么?您是否忘记执行以下操作:

global $page;