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 4092864
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:32:44+00:00 2026-05-20T19:32:44+00:00

I have a MySQL database containing a user’s country and whether they are an

  • 0

I have a MySQL database containing a user’s country and whether they are an individual or an organisation. The field names are ‘country’ and ‘type’.

Using PHP, I’d like to ‘count’ the number of countries, the number of individuals and the number of organisations in the database and then display the numbers in the following example format:

<p>So far, <strong>500</strong> individuals and <strong>210</strong> organisations from <strong>40</strong> countries have registered their support.</p>

I am currently listing the total number of records using the below code if this helps:

<?php
    $link = mysql_connect("localhost", "username", "password");
    mysql_select_db("database_name", $link);        
    $result = mysql_query("SELECT * FROM table_name", $link);
    $num_rows = mysql_num_rows($result);        
    echo "&nbsp;$num_rows\n ";  
?>

My PHP / MySQL skills are very limited so I’m really struggling with this one.

Many thanks in advance!

Ben

  • 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-05-20T19:32:45+00:00Added an answer on May 20, 2026 at 7:32 pm

    What you are looking for is a count based on a grouping. Try something like this:

    $sql = "SELECT type, count(*) as cnt FROM users GROUP BY type";
    $result = mysql_query($sql);
    
    $counts = array();
    
    while ($row = mysql_fetch_assoc($result)) {
        $counts[$row['type']] = $row['cnt'];
    }
    

    This will give you an array like

    Array (
        'individual' => 500,
        'organization' => 210
    )
    

    For counting the countries, use the first statement as posted by Hammerite.


    EDIT: added a verbose example for counting the countries

    $sql = "SELECT COUNT(DISTINCT country) AS NumCountries FROM users";
    $result = mysql_query($sql);
    
    $number_of_countries = 0;
    if ($row = mysql_fetch_assoc($result)) {
        $number_of_countries = $row['NumCountries'];
    }
    

    This altogether you can then print out:

    printf('<p>So far, <strong>%d</strong> individuals and <strong>%d</strong> '.
           'organisations from <strong>%d</strong> countries have registered '.
           'their support.</p>', $counts['individual'], $counts['organization'],
            $number_of_countries);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of tests that run against a MySQL database which is
I have a mysql Database and 2 tables let's say clients and schools. Now
I have a MySQL database that looks similar to this: ID Group Name 1
I have a mysql database with a particular table with a little over 6
Is there an osx mysql gui, which can run a script containing multiple statements,
I've successfully connected to MySQL database through Eclipse without getting tomcat involved, so at
We have a console application (currently .NET) that sends out mail in bulk to
I am building a customer sales and invoicing app for a company.The app is
Is it possible to implement the 'not-found=ignore' behaviour of NHibernate on a key mapping
Im not satisfied im doing this the best way possible. Im taking serialized code

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.