PHP 和 MySQL 问题

标签 php mysql

我正在尝试计算某篇文章被评分的次数,例如我的成员(member)对 users_articles_id 3 评分了多少次。

我还尝试计算某篇文章的分数,例如 users_articles_id 3 通过其 ratings 数据库与 ratings_id 评分总数应为 13。

我想知道我这样做是否正确,因为对我来说这看起来完全错误?我希望有人能帮我解决这个问题吗?我的代码到底应该放在哪里?

我正在使用 PHP 和 MySQL?

这是我的 MySQL 表

CREATE TABLE articles_grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ratings_id INT UNSIGNED NOT NULL,
users_articles_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id)
);


CREATE TABLE ratings (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
points FLOAT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);



数据库输入

文章评级

id  ratings_id  users_articles_id   user_id     date_created
1   3           2                   32          2010-01-13 02:22:51
2   1           3                   3           2010-01-13 02:23:58
3   2           3                   45          2010-01-13 02:24:45

评分

id      points
1       10
2       3
3       5



这是我正在尝试修复的 PHP 代码。

// function to retrieve rating
function getRating(){   
    $sql1 = "SELECT COUNT(*) 
                      FROM articles_ratings 
                      WHERE users_articles_id = '$page'";

    $result = mysql_query($sql1);
    $total_ratings = mysql_fetch_array($result);

    $sql2 = "SELECT COUNT(*) 
                            FROM ratings 
                            JOIN ratings ON ratings.id = articles_ratings.ratings_id
                            WHERE articles_ratings.users_articles_id = '$page'";

    $result = mysql_query($sql2);
    $total_rating_points = mysql_fetch_array($result);
    if(!empty($total_rating_points) && !empty($total_ratings)){
    // set the width of star for the star rating
    $rating = (round($total_rating_points / $total_ratings,1)) * 10; 
    echo $rating;
    } else {
        $rating = 100; 
        echo $rating;
    }
}

最佳答案

嗯,我认为这里有几个问题。

1) 您正在定义一个名为articles_grades 的表,而不是代码中的articles_ ratings 表。 2)为什么articles_grades和 ratings需要放在不同的表中?这些表之间存在一一对应关系。 3)您需要在第二个查询中进行 sum(points) 。 4) 您可以将两个查询合并为一个查询。

如果您不更改架构,我会这样做:

<?php

mysql_connect('localhost','root','fake123123');
mysql_select_db('test');

$result = mysql_query('SELECT users_articles_id,count(*),sum(r.points)
 FROM articles_grades ag,ratings r 
 WHERE ag.ratings_id = r.id
GROUP BY users_articles_id');

if (!$result)
  die('invalid');
else{

  echo '<table><tr><th>Article Id</th><th>#Ratings</th><th>#Points</th></tr>';
  $a = mysql_fetch_row($result);
  while($a){
    echo '<tr><td>'.implode('</td><td>',$a).'</td></tr>';

    $a = mysql_fetch_row($result);
  }
  echo '</table>';

}

?>

您可以将其作为 CGI 脚本运行。它应该返回结果表。

请告诉我这是否有帮助。

关于PHP 和 MySQL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2062028/

相关文章:

php - 如何在具有多个数据库的 CodeIgniter 中存储数据库 session ?

java - 调用存储过程 (MySQL) 从 Hibernate 中抛出异常

MySQL 的联合和排序通过帮助

mysql - 数据库和文件中的UTF8MB4?

php - 多对多查找

javascript - MySQL 的时间验证范围

PHP - 如果没有 MySQL 连接则显示简单消息

PHPmyadmin 在访问任何数据库时永远升级/加载

Android 设备发送至 GCM 消息 "Access denied for user ' root'@'localhost' "

mysql - 在 SQL 中选择句子的特殊形式