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

时间:2010-01-16 12:09:49

标签: php mysql error-handling mysqli

  

可能重复:
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

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

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

代码围绕下面列出的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)';
    }
}

2 个答案:

答案 0 :(得分:13)

如果查询中存在错误,则

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();
}

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

答案 1 :(得分:2)

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

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

PS:只是想知道究竟是什么$page?你忘了做了:

global $page;