0

我在 MySql 数据库中有这张表:

在此处输入图像描述

运行此查询后:

SELECT score, count(*) FROM Coaches group by score ORDER BY score DESC

结果表如下所示:

在此处输入图像描述

现在在 php 中,我尝试获取结果并遍历数组以确定每个教练属于哪个组并在排名中获得他的位置。因此我写了这个:

$groupsOfScoresQuery = "SELECT score, count(*) FROM Coaches group by score ORDER BY score DESC";

$result = mysqli_query($dbc, $groupsOfScoresQuery);

if ($result) {  // query did successfully run
$response['topCoaches'] = array();

    if (mysqli_num_rows($result) > 0)   {   
        while ( $rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {

            $currentRanking++;
            $score = array(); // temp user array for one group of scores
            $numberOfCoaches; // Number of coaches with this particular number of scores
            $scoresGroup; // Scores in the particular group

            $score["scores"] = $rowScore["score"];
            $score["count"] = $rowScore["count(*)"];
            $numberOfCoaches = $score["count"];
            $scoresGroup = $score["scores"];

            $response["scoresGroup"] = $scoresGroup; // HERE IS THE PROBLEM

.
.
.
more processing
} // end WHILE

为什么$response["scoresGroup"]总是包含结果中的最后一个值?在这种情况下,这是123。我认为这是通过循环的第一次迭代,并且$response["scoresGroup"]将保存第一个元素(474),在第二次迭代期间应该保存 382?我在这里做错了什么?我是否使用正确的函数来获取结果?还是我应该使用不同的循环来实现我的目标?我在这里先向您的帮助表示感谢。

4

4 回答 4

2
if (mysqli_num_rows($result) > 0)   {   
        while ( $rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {

        $currentRanking++;
        $score = array(); // temp user array for one group of scores
        $numberOfCoaches; // Number of coaches with this particular number of scores
        $scoresGroup; // Scores in the particular group

        $score[]["scores"] = $rowScore["score"];
        $score[]["count"] = $rowScore["count(*)"];
        $numberOfCoaches[] = $score["count"];
        $scoresGroup[] = $score["scores"];

        $response[]["scoresGroup"] = $scoresGroup; // HERE IS THE PROBLEM
于 2012-11-01T08:22:30.953 回答
2

查看问题的描述,您需要定义一个多维数组来存储查询结果集中的所有结果。

请参考以下代码片段

           $groupsOfScoresQuery = "SELECT score, count(*) FROM Coaches group by score ORDER BY score DESC";

         $result = mysqli_query($dbc, $groupsOfScoresQuery);

       if ($result) {  // query did successfully run
          $response['topCoaches'] = array();

          if (mysqli_num_rows($result) > 0)   {   
          while ( $rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {

          $currentRanking++;
          $score = array(); // temp user array for one group of scores
          $numberOfCoaches; // Number of coaches with this particular number of scores
          $scoresGroup; // Scores in the particular group

         $score["scores"] = $rowScore["score"];
         $score["count"] = $rowScore["count(*)"];
         $numberOfCoaches = $score["count"];
         $scoresGroup = $score["scores"];

         $response["scoresGroup"][] = $scoresGroup; //Notice the array here

         .
         .
         .
         more processing
         } // end WHILE
于 2012-11-01T08:26:55.320 回答
2

您没有发布$response;的预期结构 这是我认为您正在尝试做的事情:

while ($rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $response["scoresGroup"][] = array(
        "scores" => $rowScore["score"],
        "count" => $rowScore["count(*)"]
    );
}
// $response["scoresGroup"][0]["scores"] = 474
// $response["scoresGroup"][0]["count"]  = 1
// $response["scoresGroup"][1]["scores"] = 382
// $response["scoresGroup"][1]["count"]  = 1
// $response["scoresGroup"][2]["scores"] = 123
// $response["scoresGroup"][2]["count"]  = 1

也许:

while ($rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $response["scoresGroup"][$rowScore["score"]] = $rowScore["count(*)"]
}
// $response["scoresGroup"][474] = 1
// $response["scoresGroup"][382] = 1
// $response["scoresGroup"][123] = 1
于 2012-11-01T08:27:28.867 回答
1

每次运行循环时,您都在设置 $response['scoresGroup'] ,因此最后它只包含最后一个元素。尝试更改您在每个循环中放入数据的变量。

$x++;
$response['scoresGroup' . x] = $scoresGroup;
于 2012-11-01T08:19:46.607 回答