Mysql query with PHP brings duplicate results

0

I'm trying to create a code in php to build a ranking, I was able to make the data appear, the problem is that it's duplicating the results.

$rank = mysql_query("SELECT * FROM user_quizzes ORDER BY pass_score_point DESC") or die(mysql_error());

    $i = 0;
    while($row = mysql_fetch_assoc($rank)){
        $i++;

$rank2 = mysql_query("SELECT * FROM users WHERE not UserName='admin' ORDER BY UserName DESC") or die(mysql_error());
    $i2 = 0;
    while($row2 = mysql_fetch_assoc($rank2)){
        $i2++;

$result = mysql_query('SELECT SUM(pass_score_point) AS pass_score_point FROM user_quizzes' ); 
$row3 = mysql_fetch_assoc($result); 
$sum = $row3['pass_score_point'];       


?>


        <div class="topo">
         <?php echo $row2['UserName']; ?> com <?php echo $row3['pass_score_point']; ?> pontos de acertos! 
        </div>
    
asked by anonymous 20.06.2016 / 19:30

2 answers

1

In your query: [SELECT SUM (pass_score_point) AS pass_score_point FROM user_quizzes] you have to group by some column with GROUP BY Ex.: SELECT userName, SUM (pass_score_point) AS pass_score_point FROM user_quizzes Group By userName

I do not know what field you will need to group but aggregate functions like SUM () must have group by.

Below is the syntax of GROUP By:

SELECT column_name, aggregate_function (column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;

    
20.06.2016 / 20:31
0

try doing this in your query

$rank2 = mysql_query("SELECT * FROM users WHERE userID='".$row['user_id']."' LIMIT 0,1") or die(mysql_error());

and see if the duplicate problem has been resolved

    
20.06.2016 / 19:44