我一直收到以下错误,我想知道如何修复?
这是我第二次遇到这个错误,我第一次修复它但是由于某种原因我第二次无法修复它。
Fatal error: Unsupported operand types on line 103
这是第103行。
$avg = (round($total_rating_points / $total_ratings,1));
以下是完整的代码。
function getRatingText(){
$dbc = mysqli_connect ("localhost", "root", "", "sitename");
$page = '3';
$sql1 = "SELECT COUNT(users_articles_id)
FROM articles_grades
WHERE users_articles_id = '$page'";
$result = mysqli_query($dbc,$sql1);
if (!mysqli_query($dbc, $sql1)) {
print mysqli_error($dbc);
return;
}
$total_ratings = mysqli_fetch_array($result);
$sql2 = "SELECT grade_points
FROM grades
JOIN articles_grades ON grades.id = articles_grades.grade_id
WHERE articles_grades.users_articles_id = '$page'";
$result = mysqli_query($dbc, $sql2);
if (!mysqli_query($dbc, $sql2)) {
print mysqli_error($dbc);
return;
}
while($row = mysqli_fetch_array($result)) {
$trp[] = $row[0];
}
$total_rating_points = array_sum($trp);
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)';
}
}
答案 0 :(得分:51)
$total_ratings
是一个数组,不能用于分组。
从上面:
$total_ratings = mysqli_fetch_array($result);
答案 1 :(得分:5)
$total_ratings
是一个数组。
答案 2 :(得分:2)
我在以下代码中遇到了类似的错误: -
foreach($myvar as $key => $value){
$query = "SELECT stuff
FROM table
WHERE col1 = '$criteria1'
AND col2 = '$criteria2'";
$result = mysql_query($query) or die('Could not execute query - '.mysql_error(). __FILE__. __LINE__. $query);
$point_values = mysql_fetch_assoc($result);
$top_five_actions[$key] += $point_values; //<--- Problem Line
}
事实证明我的$ point_values变量偶尔会返回false导致问题所以我通过将其包装在mysql_num_rows中来修复它: -
if(mysql_num_rows($result) > 0) {
$point_values = mysql_fetch_assoc($result);
$top_five_actions[$key] += $point_values;
}
不确定这是否有帮助?
干杯
答案 3 :(得分:0)
我想你想这样做:
$total_rating_count = count($total_rating_count);
if ($total_rating_count > 0) // because you can't divide through zero
$avg = round($total_rating_points / $total_rating_count, 1);