gpt4 book ai didi

php - ajax-php 星级评分系统(仅显示来自 mysql 数据库的平均日期)

转载 作者:搜寻专家 更新时间:2023-10-31 21:12:38 24 4
gpt4 key购买 nike

我需要一个评级系统,用 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;
?>

我遇到这样的错误:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\wamp\www\final work_apr51\final work_apr51\calculation.php on line 19

Warning: Division by zero in C:\wamp\www\final work_apr51\final work_apr51\calculation.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>";
}

关于php - ajax-php 星级评分系统(仅显示来自 mysql 数据库的平均日期),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15967369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com