2

我需要一个评级系统,它显示 php 中的平均评级。我完成了评级过程(保存和更新过程)。我只需要在 php 中显示平均评分(使用 php-Ajax 评分系统)。

当我从数据库中检索数据时,我遇到了错误。代码是这样的:

<?php
    $con = mysql_connect("localhost","root","");
    if(!$con){
        echo "Connection to the Database Console was Unsuccessful";
    }
    $select = mysql_select_db("oilandgas13",$con);
    if(!$select){
        echo "Connection to the Database was Unsuccessful";
    }

$add_coun= "SELECT sum(rating) sum, count(id) count from comments WHERE item_id = $itemID AND status=1";
        
        $result = mysql_query($add_coun,$con);
        if(!$result)
        {
        echo "query was not successfully";  
        }
        
        $result = mysql_fetch_object($result);
        
        $sum = $result->sum;
        $count = $result->count;
        $rating = $sum / $count;
        
        echo $rating;
?>

我收到这样的错误:

警告:mysql_fetch_object():提供的参数不是第 19 行 C:\wamp\www\final work_apr51\final work_apr51\calculation.php 中的有效 MySQL 结果资源

警告:第 23 行 C:\wamp\www\final work_apr51\final work_apr51\calculation.php 中除以零

4

1 回答 1

2

也许这可以帮助你?

    $link = mysqli_connect("localhost","mysqlusername","mysqlpassword","dbname");
    $rating = mysql_real_escape_string($_GET['id']);
    $q = mysqli_query($link,"SELECT * FROM ratings WHERE id='{$rating}'"); //Get our ratings by the page that has rated

    //Die if id dont exist!
    if(mysqli_num_rows($q) == 0) die("Wrong page id!");


    //Select good & bad ratings
    $good = mysqli_query($link,"SELECT * FROM ratings WHERE id='{$rating}' AND value ='yes'");
    $bad = mysqli_query($link,"SELECT * FROM ratings WHERE id='{$rating}' AND value ='no'");

    //Count good & bad ratings
    $gcnt = mysqli_num_rows($good);
    $bcnt = mysqli_num_rows($bad);

    //Calculate
    $totalVotes = $gcnt + $bcnt;

    if($totalVotes == 0){
      echo $totalVotes." votes";
    }
    if($totalVotes > 0){
      echo "<font color='green'>".$totalVotes." votes</font>";
    }
    if($totalVotes < 0){
      echo "<font color='red'>".$totalVotes." votes</font>";
    }        
于 2013-04-12T09:53:29.987 回答