php 致命错误:不支持的操作数类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2077618/
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
Fatal error: Unsupported operand types
提问by tEcHnUt
I Keep getting the following error and I was wondering on how to fix it.
我不断收到以下错误,我想知道如何解决它。
Fatal error: Unsupported operand types on line 97
Its around this area of code listed below. I can list the full code if needed.
它围绕下面列出的代码区域。如果需要,我可以列出完整的代码。
PHP code
PHP代码
$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)';
}
Here is Line 97
这是第 97 行
$avg = (round($total_rating_points / $total_ratings,1));
Here is the full code.
这是完整的代码。
function getRatingText(){
$dbc = mysqli_connect ("localhost", "root", "", "sitename");
$page = '3';
$sql1 = "SELECT COUNT(*)
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 COUNT(*)
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;
}
$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 just somebody
$total_rating_pointsis an array. you cannot divide it by a number.
$total_rating_points是一个数组。你不能把它除以一个数字。
回答by SoapBox
(edited since the MySQL query was added to the question)
(因为 MySQL 查询被添加到问题中进行了编辑)
You've used mysql_fetch_arrayto get your result from MySQL, as the name suggests this returns an array. You can't do math like that on arrays.
您曾经mysql_fetch_array从 MySQL 获取结果,顾名思义,这会返回一个数组。你不能在数组上做那样的数学运算。
You want to change your MySQL query to this:
您想将 MySQL 查询更改为:
$sql1 = "SELECT COUNT(*) as count
FROM articles_grades
WHERE users_articles_id = '$page'";
Change your mysql_fetch_array code to this:
将您的 mysql_fetch_array 代码更改为:
$total_rating_points = mysql_result($result, 0, "count");
That will return the actual number, which you can then use for math. Change both of your queries to this format and you should be good to go.
这将返回实际数字,然后您可以将其用于数学。将您的两个查询更改为这种格式,您应该很高兴。
回答by nickf
That's usually caused when the type of one of your variables is something you're not expecting it to be.
这通常是由于您的变量之一的类型是您不期望的。
$x = "8";
$y = 4;
$x / $y; // this is OK, PHP converts "8" to 8
$x = new FooObject();
$y = 4;
$x / $y; // PHP says "wtfmate??"
Dump out the variables $total_rating_pointsand $total_ratings:
转储变量$total_rating_points和$total_ratings:
var_dump($total_rating_points);
var_dump($total_ratings);
var_dump($total_rating_points);
var_dump($total_ratings);
.. then work backwards through your code (checking debug_backtrace()if required) to find where it went awry.
.. 然后通过您的代码向后工作(检查debug_backtrace()是否需要)以找到它出错的地方。
Edit: duhhh.. I just looked at your code again and saw the issue. The $total_rating_pointsis an array, and this is the cause of the problem. How do you divide an array by a number? Answer: you can't.
编辑:duhhh .. 我刚刚又看了你的代码,看到了这个问题。这$total_rating_points是一个数组,这就是问题的原因。如何将数组除以数字?回答:你不能。
Do you want to divide each of the members of the array by $total_ratings? eg:
您想将数组的每个成员除以$total_ratings吗?例如:
// input: [2, 4, 6, 16] / 2
// output: [1, 2, 3, 8]
function divideArrayMembers($arr, $denominator) {
$ret = array();
foreach ($arr as $key => $val) {
$ret[$key] = $val / $denominator;
}
return $ret;
}
...or do you want to divide the total?
……还是要除以总数?
input: [2, 4, 6, 16] / 2
output: 14 // (2 + 4 + 6 + 16) / 2
function divideTotal($arr, $denominator) {
return array_sum($arr) / $denominator.
}
回答by Alix Axel
Use array_sum()on $total_rating_pointsand $total_ratings.
array_sum()在$total_rating_points和 上使用$total_ratings。
回答by pradeep
if($_REQUEST['cityCode'] != ''){
$_SESSION['Cri_locations'] = $_REQUEST["s"];
$_SESSION['Cri_DateFrom'] = $_REQUEST["startDate"];
$_SESSION['Cri_DateTo'] = $_REQUEST["endDate"];
$what_to_search_id =str_replace("AREA-","",$_REQUEST["cityCode"]);
$total_adults =0;
$total_childs =0;
$child_ages =0;
$no_of_room =0;
for($i=1;$i<=$_REQUEST["Cri_noofRooms"];$i++){
$check_adults =$_REQUEST['adults-r'.$i.''];
if($check_adults>0){
$adults =$_REQUEST['adults-r'.$i.''];
$childs =$_REQUEST['childs-r'.$i.''];
$childAge =$_REQUEST['childAge-r'.$i.''];
$packs_arr[] =array('adults'=>$adults,'childs'=>$childs,'childAge'=>$childAge);
$total_adults =$total_adults+$adults;
$total_childs =$total_childs+$childs;
$child_ages =$child_ages+$childAge;//error accours here pls help
$no_of_room =$no_of_room+1;
}
}

