从php中的mysql查询中获取结果数组,同时循环问题

时间:2012-11-01 08:13:49

标签: php mysql

我在MySql db中有这个表:

enter image description here

运行此查询后:

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

结果表如下所示:

enter image description here

现在在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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分:1)

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

$x++;
$response['scoresGroup' . x] = $scoresGroup;