我有问题选择选择SQL Max和组语法,然后将其设置为变量。我选择最高分是没有问题的。但是我想回复拥有该分数和Id的用户以重定向到他的个人资料页面
This is my Database
-----------------------------
id | user | score | justplayed
1 | player1 | 1000 | 1
2 | player2 | 1000 | 0
PHP代码$sql = "SELECT MAX(score) AS max_score FROM score GROUP BY score WHERE justplayed > 0 ";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$highscore = $row["max_score"];
$highscoreowner = $row["user"];
$highscoreownerid = $row["id"];
}
}
Html代码
Player of the day
<? echo $highscoreowner; ?>
Score
<? echo $highscore; ?><br>
<a href="profile.php?id=<? echo $highscoreownerid; ?>
您只在SQL查询中选择了最高分,因此无法在PHP中提取其他值。如果要查找分数最高的整行,则使用以下查询:
SELECT score, user, id
FROM score
WHERE justplayed > 0
ORDER BY score DESC
LIMIT 1
这将获取所有行,将它们从最高分数到最低分数排序,然后只返回第一行。