简体   繁体   中英

Strange behavior of a php/mysql function

I'm working on a project where I have to show the leaderboard of players. But when wrote the below code, It shows the ranks with 2, 4, 6, 8, ... But not the odd numbered ranks. Can anyone tell me "What's wrong in this ?"

$query1_string = "CREATE VIEW Leaderboard AS SELECT Name, Points, PhoneNo
                  FROM user ORDER BY Points DESC";
$query2_string = "set @rank = 0";
$query3_string = "SELECT @rank := @rank + 1 as Rank, Name, Points
                  FROM Leaderboard";
$query5_string = "DROP VIEW Leaderboard";

// Doing the queries
$query1 = mysqli_query($con, $query1_string) or die(mysqli_error($con));
$query2 = mysqli_query($con, $query2_string) or die(mysqli_error($con));
$query3 = mysqli_query($con, $query3_string) or die(mysqli_error($con));
// Initializing the count
$count = 0;

//Making an array of strings including Rank, Name and Points of the Top 5 Players
while (($count < 5) && (mysqli_fetch_array($query3, MYSQL_NUM))) {
    $row = mysqli_fetch_array($query3, MYSQL_NUM);
    $results[$count] = $row[0] . "   " . $row[1] . "    " . $row[2];
    $count++;
}

// Dropping the view.
$end_query = mysqli_query($con, $query5_string) or die(mysqli_error($con));

//Returning the array
$leader = implode("\n", $results);
echo $leader;

You are executing the query3 two times and displying the data retrieved from second execution

while (($count < 5) && (mysqli_fetch_array($query3, MYSQL_NUM))) {
    $row = mysqli_fetch_array($query3, MYSQL_NUM);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM