PHP - 致命错误:不支持的操作数类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2108875/
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
PHP - Fatal error: Unsupported operand types
提问by tEcHnUt
I keep getting the following error and I was wondering on how to fix?
我不断收到以下错误,我想知道如何解决?
This is the second time I got this error I fixed it the first time but for some reason I cant fix it the second time.
这是我第二次遇到这个错误,我第一次修复了它,但由于某种原因我第二次无法修复它。
Fatal error: Unsupported operand types on line 103
Here is line 103.
这是第 103 行。
$avg = (round($total_rating_points / $total_ratings,1));
Here is the full code below.
这是下面的完整代码。
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)';
}
}
回答by Pekka
$total_ratingsis an array, which you can't use for a division.
$total_ratings是一个数组,不能用于除法。
From above:
从上面:
$total_ratings = mysqli_fetch_array($result);
回答by SilentGhost
$total_ratingsis an array.
$total_ratings是一个数组。
回答by TeeHays
I had a similar error with the following code:-
我在以下代码中遇到了类似的错误:-
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
}
It turned out that my $point_values variable was occasionally returning false which caused the problem so I fixed it by wrapping it in mysql_num_rows check:-
事实证明,我的 $point_values 变量偶尔会返回 false 这导致了问题,所以我通过将它包装在 mysql_num_rows 检查中来修复它:-
if(mysql_num_rows($result) > 0) {
$point_values = mysql_fetch_assoc($result);
$top_five_actions[$key] += $point_values;
}
Not sure if this helps though?
不确定这是否有帮助?
Cheers
干杯
回答by r3zn1k
I guess you want to do this:
我猜你想这样做:
$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);

