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 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

Have a PHP/MySQL form with a dropdown box containing a list of 350 names.
I have a MySQL database with words containing accents in Spanish (áéíóú). I'd like
I'm building a Route Planner Webapp using Spring/Hibernate/Tomcat and a mysql database, I have
I have a pre-existing mysql database containing around 50 tables. Rather than hand code
I have MySQL database with two tables: users and items. They look like this:
I have a MySQL database in which I have to update a certain field
Suppose I have a database containing 3 tables in a grails application: User Activity
I have a MySQL table containing userID , score . A single user can
I am using MySQL fulltext and PHP (codeigniter) to search a database containing RSS
I have a mysql database containing several items and I want to change top

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.