Php编号每个查询结果

时间:2014-08-02 17:37:42

标签: php mysqli

我正在制作排行榜,我想在html表格字段中将其编号为第一名第二名等。

如何为每一行信息编号? 就像是 $result + 1

我的查询

$result = mysqli_query($con,"SELECT * FROM playerdata WHERE Admin='0' ORDER BY Bank DESC LIMIT 1");

                            while($row = mysqli_fetch_array($result)) {
$bank = $row['bank'];

}

感谢。

2 个答案:

答案 0 :(得分:2)

您可以使用以下方法对查询结果进行排序:

$result = mysqli_query($con,"SELECT * FROM playerdata WHERE Admin='0' ORDER BY Bank DESC LIMIT 900");
$rows = mysqli_num_rows($result);

for($x = 1; $x <= $rows; $x++) {
    $row = mysqli_fetch_array($result);
    echo "#{$x} " . $row['bank'];
}

这将开始倒计时,显示“#1 ...#2 ...#3 ...”等。您也可以使用while循环执行此操作:

$result = mysqli_query($con,"SELECT * FROM playerdata WHERE Admin='0' ORDER BY Bank DESC LIMIT 900");

$x = 1;
while($row = mysqli_fetch_array($result)) {
    $bank = $row['bank'];
    echo "#{$x} " . $bank;
    $x++;
}

答案 1 :(得分:0)

使用查询本身你可以构建结果,我认为这是你需要的。不确定

SELECT    @curRank := @curRank + 1 AS rank, a.*
FROM      playerdata a, (SELECT @curRank := 0) r
WHERE Admin='0' ORDER BY Bank DESC LIMIT 900

php

$result = mysqli_query($con,"SELECT    @curRank := @curRank + 1 AS rank, a.*
    FROM      playerdata a, (SELECT @curRank := 0) r
    WHERE Admin='0' ORDER BY Bank DESC LIMIT 900");

while($row = mysqli_fetch_array($result)) {
    $rank = $row['rank'];
    $bank = $row['Bank'];
}