Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8792979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:03:42+00:00 2026-06-13T23:03:42+00:00

I have this table in MySql db: After running this query: SELECT score, count(*)

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:03:44+00:00Added an answer on June 13, 2026 at 11:03 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query that I'm running against mysql: SELECT video.title AS title, video.url
I have the following sample query (MySQL): SELECT * FROM `action` WHERE `customer_id` IN
I'm running a query to select an array of id's from one table, so
I have a single DB table (MySQL) which I am running a simple SELECT
I have this mysql table called comments which looks like this: commentID parentID type
I have a MySQL table called accounts . Within this table is a field
i have a t_class table in mySql, in this table there are 3 columns,
I have this in a MySQL db: table Message sender_id int recipient_id int created
I have MySQL InnoDB table utf-8 encoded. This table has only id and name
Let's say I have a MySQL table that is something like this: software table:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.