Problem with ("select username, points from date order by points desc limit 10");

0

I'm trying to make a ranking system, where the user name and punctuation appears. I'm doing the following:

$select = $mysqli->query("select username, pontos from data order by pontos desc limit 10");
  $row = $select->num_rows;
  $get = $select->fetch_array();
  $top_1 = $get[0];
  $top_1_pontos = $get[1];
  $top_2 = $get[2];
  $top_2_pontos = $get[3];

Using an echo in $top_1 it displays the name of the user with the highest score.

Using an echo in $top_1_pontos it displays the user's score.

From $top_1_pontos it does not display anything else ... what did I do wrong?

What is the correct way to get the ranking by searching all users and listing the top 10?

MySql looks something like this:

INSERT INTO 'data' ('id','username','pontos') VALUES 
(1,'Joao',9321),
(2,'Maria',151000),
(3,'Pedro',21201),
(4,'Jose',31252),
(5,'Antonio',131145),
(6,'Adriano',13211),
(7,'Marcelo',6897),
(8,'Juliana',53144);
    
asked by anonymous 08.06.2016 / 06:32

1 answer

1

You need 1 fetch to read each record. Therefore, repeat the fetch after showing the top_1 data. (and use indexes 0 and 1 again)

If you want to show all 10, it's more practical to put your fetch in the while condition.

EDIT: As you updated your question, I update my answer. Here is the code snippet in question:

while($get = $select->fetch_array()) {
  $nome = $get[0];
  $pontos = $get[1];
  //faz algo com $nome e $pontos
}

This will read the names and scores of the top 10.

    
08.06.2016 / 15:25