Marcin S.
Marcin S.

Reputation: 11191

Fetching the result array from mysql query in php, while loop issue

I have this table in MySql db:

enter image description here

After running this query:

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

The result table look like this:

enter image description here

Now in php I try to fetch the result and iterate through the array to determine which group each coach belongs to and get his place in the ranking. Therefore I wrote this:

$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

Why $response["scoresGroup"] will always conatins the last value from the result? In this case this is 123. I thought that this is the first iteration through the loop and $response["scoresGroup"] wll hold first element (474), during the second iteration should hold 382 ? What I'm doing wrong here? Do I use correct function to fetch result? or should I use different loop to acheive my goal? Thanks for the help in advance.

Upvotes: 0

Views: 4293

Answers (4)

Salman Arshad
Salman Arshad

Reputation: 272066

You did not post the expected structure of $response; here is what I think you are trying to do:

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

Or perhaps:

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

Upvotes: 2

Rubin Porwal
Rubin Porwal

Reputation: 3845

Looking at the description of your question, you need to define a multidimensional array for storing all the results from query resultset.

Please refer the below code snippet

           $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

Upvotes: 2

Arun Killu
Arun Killu

Reputation: 14233

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

Upvotes: 2

Matt Clark
Matt Clark

Reputation: 28579

You are settings $response['scoresGroup'] each time you run the loop, so at the end, it will contain only the last element. Try changing the variable you put the data into on each loop.

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

Upvotes: 1

Related Questions