2010-01-16 81 views
7

我繼續收到以下錯誤,並且想知道如何解決它。致命錯誤:不支持的操作數類型

Fatal error: Unsupported operand types on line 97 

它圍繞下面列出的代碼區域。如果需要,我可以列出完整的代碼。

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

這裏是97號線

$avg = (round($total_rating_points/$total_ratings,1)); 

下面是完整的代碼。

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

回答

18

$total_rating_points是一個數組。你不能用數字來劃分它。

+0

我該如何解決這個問題,因爲我需要劃分$ total_rating_points/$ TOTAL_RATINGS – tEcHnUt 2010-01-16 14:39:13

+1

$ TOTAL_RATINGS [0]/$ total_rating_points [0]。您可能還想檢查Count(*)是否是兩個查詢的正確聚合函數。 – VolkerK 2010-01-16 14:57:24

1

這通常是因爲某個變量的類型是您不希望的變量的類型。

$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出來的變量 $total_rating_points$total_ratings

var_dump($total_rating_points); 
var_dump($total_ratings); 

..然後通過您的代碼反向工作(檢查 debug_backtrace()如果需要的話)來找到它偏差去。

編輯:duhhh ..我只是看了你的代碼,看到了問題。 $total_rating_points是一個數組,這是問題的原因。你如何用一個數字來分割一個數組?答:你不能。

是否要將數組中的每個成員除以$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; 
} 

...或者你想分總數嗎?

input: [2, 4, 6, 16]/2 
output: 14 // (2 + 4 + 6 + 16)/2 

function divideTotal($arr, $denominator) { 
    return array_sum($arr)/$denominator. 
} 
2

(編輯因爲MySQL查詢被添加到這個問題)

您已經使用mysql_fetch_array從MySQL得到你的結果,顧名思義這將返回數組。你不能在數組上進行數學運算。

你想你的MySQL查詢改成這樣:

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

更改mysql_fetch_array代碼如下:

$total_rating_points = mysql_result($result, 0, "count"); 

,將返回的實際數量,然後你就可以使用數學。將這兩個查詢都改爲這種格式,你應該很好。

1

使用array_sum()$total_rating_points$total_ratings

0
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

致命錯誤:不支持的操作數類型(其即將到來) – pradeep 2016-08-04 04:52:02

相關問題