开发者

Fatal error: Unsupported operand types

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

$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

$avg = (round($total_rating_points / $total_ratings开发者_StackOverflow,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)';
    }
}


$total_rating_points is an array. you cannot divide it by a number.


(edited since the MySQL query was added to the question)

You've used mysql_fetch_array to get your result from MySQL, as the name suggests this returns an array. You can't do math like that on arrays.

You want to change your MySQL query to this:

$sql1 = "SELECT COUNT(*) as count
         FROM articles_grades 
         WHERE users_articles_id = '$page'";

Change your mysql_fetch_array code to this:

 $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.


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_points and $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.

Edit: duhhh.. I just looked at your code again and saw the issue. The $total_rating_points is an array, and this is the cause of the problem. How do you divide an array by a number? Answer: you can't.

Do you want to divide each of the members of the array by $total_ratings? eg:

// 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.
}


Use array_sum() on $total_rating_points and $total_ratings.


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;

        }
    } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜