我需要一个评级系统,显示平均评级在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():所提供的参数在C:wampwwwfinal work_apr51final work_apr51calculation.php中不是一个有效的MySQL结果资源
警告:C:wampwwwfinal work_apr51final work_apr51calculation.php on line 23
也许这能帮到你?
$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>";
}