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

  • SEARCH
  • Home
  • 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 7864747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:52:43+00:00 2026-06-02T23:52:43+00:00

Using php 5.3 and mysqli I return a result set from a query that

  • 0

Using php 5.3 and mysqli I return a result set from a query that just has usernames, something like

$query_username = "SELECT username FROM some_table WHERE param = 1";
$username =  $mysqliObject->query($query_username);

while($row_username = $username->fetch_object()){
print "<br>Username: $row_username->username";
}

All fine, but here is my problem, there are repeated usernames, and I don’t know which names are going to be in the query before hand, could be bob, sue, james. Or it could be tom, dick, harry, tom. What I need to do is print out each username and how many times it shows up in this object. For very strange reasons I CANNOT use neat stuff like group by and count(*) in the query(don’t ask it is truly weird). So my question is, what is the fastest way to loop through the returned object(or associative array if need be) to get each unique name and how many times it appears. Thanks for your help and I apologize if this is a freshman CS question, I’m self taught and always filling in the gaps!

  • 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-02T23:52:44+00:00Added an answer on June 2, 2026 at 11:52 pm

    If you really must do it on the PHP side instead of using a GROUP BY clause:

    while($row_username = $username->fetch_object())
    {
        if(isset($usernames[$row_username['username']]))
        {
            $usernames[$row_username['username']]++;
        }
        else
        {
            $usernames[$row_username['username']] = 1;
        }
    }
    
    asort($usernames);
    // use ksort() to sort by username instead of the count
    
    // print out the usernames
    foreach($usernames as $username => $count)
    {
        echo $username . ", count: " . $count;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm using fullCalendar 1.5.1, and I've created a PHP query that returns almost
When querying using mysqli_stmt::prepare() and execute() , it will not return the result set.
I'm using the following mysql query to return some data from my database. Currently
I'm used to using the command mysql_insert_id() in PHP to return the id of
I'm using MySQLi with PHP(5.2.4) MySQL(5.0.51a) on Ubuntu 8.04 LAMP. The relevant Db tables
I'm using prepared statements and MySQLi, and am currently getting this error. PHP Catchable
I have a php page that pulls data from a mysql database based on
Server details: PHP v5.3.5 Using MySQLi library client api version: mysqlnd 5.0.7-dev - 091210
I was writing a database handler class in PHP using the mysqli class and
Using PHP / MySQL all encoded up as UTF, we have recently had to

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.