Mysql query with PHP brings duplicate results

Asked

Viewed 456 times

-1

I’m trying to create a code in php to build a ranking, I managed to make the data appear, the problem is that is 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>
  • Post the rest of the code where you search the data...

  • Sorry I didn’t see, that I hadn’t gone, ready

  • put your database query

  • remove your connection data if it will not enter your db

  • I’ve already taken, thank you

  • Those whiles there are weird are all chained up? use the button { } to format the code.

  • you better exchange your password in favor only for security issues

Show 2 more comments

2 answers

1

In your consultation: A [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 don’t know why field you have to group but aggregation functions like SUM() have to 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;

  • It didn’t help, I should take out Assoc ? Because the error in the line below

  • passes the user_quizzes table structure and the query you executed.

0

try to do 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 solved

  • 1

    Yes, but thinking that there will be several students, with different results..

  • you can in your example [link]http://www.rfsys.com.br/prova/rank.php put more students so that you can see if this repeating in your query, you have already checked if in your db is more of an equal result ?

  • @Very likely Ricardogoncalves you have something repeated in your database, because you in your query make it to bring all the records of the database.

  • started to appear duplicated, after I made the sum command of the fields, because in the case there, he answered two proofs, one drew 9 and the other 7, if I take the sum field, appears the correct result.. even though he answered different tests...

  • @Ricardogoncalves I’m checking here

  • Thanks for the help

  • I’m trying to recreate your code here but it’s not working very well, it’s badly formatted, if you want to send me this part in my email contact ?

  • No, how would the sum in php?

  • right which fields and which banks you want to add example I see that you want to do the sum of the user Quizzer pass_score_point plus what other field

  • i changed the above answer test it and see if it will give any result

  • Nop, still the same

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.