2010-01-21 18 views
14

Sigo recibiendo el siguiente error y me preguntaba cómo solucionarlo.PHP - Error fatal: tipos de operandos no admitidos

Esta es la segunda vez que recibo este error lo arreglé la primera vez pero por alguna razón no puedo solucionarlo la segunda vez.

Fatal error: Unsupported operand types on line 103 

Aquí es la línea 103.

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

Aquí está por debajo del código completo.

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

Creo que acabo de resolverlo gracias a todos! – tEcHnUt

+6

¿Olvidó la contraseña de su cuenta anterior? vea http://stackoverflow.com/questions/2077618/php-fatal-error-unsupported-operand-types y http://stackoverflow.com/users/252134/technut – VolkerK

Respuesta

41

$total_ratings es una matriz, que no se puede usar para una división.

Desde arriba:

$total_ratings = mysqli_fetch_array($result); 
0

supongo que quieres hacer esto:

$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); 
2

que tenían un error similar con el siguiente código: -

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  
} 

Resultó que mi variable $ point_values ​​ocasionalmente se vuelve falsa que causó el problema así que lo arreglé envolviéndolo en mysql_num_rows check: -

if(mysql_num_rows($result) > 0) { 
     $point_values = mysql_fetch_assoc($result); 
     $top_five_actions[$key] += $point_values; 
} 

No seguro si esto ayuda sin embargo?

Cheers

Cuestiones relacionadas