0

我目前正在用 PHP 创建一个页面,该页面将显示用户及其在我网站的高分中的排名。

我遇到了一个问题,我的 MySQL 查询没有按预期运行,但是,@VladBalmos 在这里解决了我的问题:MySQL Highscores - User's Personal Ranks: Duplicate Entries Causeing Incorrect Values

现在我相信我的 MySQL 查询正在运行,我需要解决我的 PHP 代码出现的问题。这是我的代码:

$userid = (int) $_GET['searched'];
    for ($i = 0; $i < 5; $i++) {
        if ($i == 0) {
            $skill_query = mysqli_query($database, "SELECT uid, overall, overallxp, gamelevel FROM playerstats WHERE uid = ". $userid ." ORDER BY playerstats.overall DESC, playerstats.overallxp DESC") or print(mysqli_error($database));
            $skill_array = mysqli_fetch_array($skill_query);
            //old rank query: $rank_query = mysqli_query($database, "SELECT count(*) + 1 FROM (SELECT uid, overall, overallxp, gamelevel FROM playerstats GROUP BY playerstats.uid) AS x WHERE overall > (SELECT overall FROM playerstats WHERE uid = ". $userid .")") or print(mysqli_error($database));
            $rank_query = mysqli_multi_query($database, "SET @rank=0; SELECT rank, uid, overall, overallxp FROM (SELECT @rank:=@rank + 1 AS rank, uid, overall, overallxp FROM playerstats ORDER BY overall DESC, overallxp DESC) as tmp") or print(mysqli_error($database));
            $rank_array = mysqli_fetch_array($rank_query);
        } else {
            $skill_query = mysqli_query($database, "SELECT uid, ". $skills[$i] .", gamelevel FROM playerstats WHERE uid = ". $userid ." ORDER BY playerstats.". $skills[$i] ." DESC, playerstats.gamelevel DESC") or print(mysqli_error($database));
            $skill_array = mysqli_fetch_array($skill_query);
            $rank_query = mysqli_query($database, "SELECT count(*) + 1 FROM (SELECT uid, ". $skills[$i] .", gamelevel FROM playerstats GROUP BY playerstats.uid) AS x WHERE ". $skills[$i] ." > (SELECT ". $skills[$i] ." FROM playerstats WHERE uid = ". $userid .")") or print(mysqli_error($database));
            $rank_array = mysqli_fetch_array($rank_query);
        }
    
        echo "<tr class='hs_row'>";
        echo "<td align='center'>";
        echo $rank_array[0]; //rank output
        echo "</td>";
        echo "<td align='center'>";
        echo $skill_array[1]; //skilllevel
        echo "</td>";
        echo "<td align='center'>";
        if ($i == 0) {
            echo number_format($skill_array[2]); //skillxp
        } else {
            echo number_format($skill_array[1]); //skillxp
        }
        echo "</td></tr>";
    }

...我收到的错误是:

警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在第 156 行的 [path]/personal.php 中给出

命令不同步;你现在不能运行这个命令

警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在第 159 行的 [path]/personal.php 中给出

命令不同步;你现在不能运行这个命令

警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在第 161 行的 [path]/personal.php 中给出

命令不同步;你现在不能运行这个命令

所以,我的问题是,我该如何纠正这个错误?

4

1 回答 1

1
$rank_query = mysqli_multi_query($database, $yourQuery);
if($rank_query) {
    while(mysqli_next_result($dabatase)) {
        if($result = mysqli_store_result($database)) {
            while($row = mysqli_fetch_row($result) {
                // do something
            }
        }

    }
}

您必须遍历多个查询的结果,然后访问每个结果集的行。由于第一个查询只是设置了一个变量,因此您必须跳到下一个结果集并获取实际行(这就是我这样做的原因 *while(mysqli_next_result...*) 阅读php 手册中的文档

于 2012-08-23T14:11:55.283 回答