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

2021-12-25 00:00:00 error-handling php mysql mysqli

我收到下面列出的警告,我想知道如何修复它

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

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

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

// 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)';
    }
}

推荐答案

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

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

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

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

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

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

相关文章