Page 1 of 1

Show total results from rating in mysql

Posted: 22 Feb 2017, 18:34
by JanMolendijk
I have a rating script where new people able to rate from 1 till 5.
Now i have this simple code what shows the total number of rates:

Code: Select all

<?php
include('../connection.php');

$article_id = (int) $_GET['article_id'];

$result = $conn->query("SELECT COUNT(*) FROM `article_rating` WHERE article_id='" . $article_id . "'");
$row = $result->fetch_row();
echo 'My Score ', $row[0];
?>
I wanna have the total score which the people rated on the article_id.
this is my data-base

Code: Select all

article_id  rating
I hope you can help me out :)

Show total results from rating in mysql

Posted: 23 Feb 2017, 05:43
by Admin
Hello
If you want to get the value of the total ratings in the "rating" column, you can use the mysql SUM() instruction.

Code: Select all

$sql ="SELECT SUM(rating) AS t_rate FROM article_rating WHERE article_id=". $article_id;

Show total results from rating in mysql

Posted: 23 Feb 2017, 08:27
by JanMolendijk
Thank you so mutch Admin great work