[英]ajax-php star rating system (just show the average date from mysql databse)
我需要一个评级系统来显示 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 结果资源
警告:在 C:\\wamp\\www\\final work_apr51\\final work_apr51\\calculation.php 第 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>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.