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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:56:25+00:00 2026-06-13T12:56:25+00:00

I have two tables in my database table: products table: companies +———–+————+ +——+————+ |

  • 0

I have two tables in my database

table: products                   table: companies
+-----------+------------+        +------+------------+
| name      | company_id |        | id   | name       |
+-----------+------------+        +------+------------+
|Product 1  | 1          |        | 1    | Company 1  |
|Product 2  | 1          |        | 2    | Company 2  |   
|Product 3  | 3          |        | 3    | Company 3  |
|Product 4  | 1          |        | 4    | Company 4  |
|Product 5  | 1          |        | ...               |
|Product 6  | 3          |        +------+------------+
|Product 7  | 2          |
|...                     |
+-----------+------------+

Now I have to make company-selector (filtering products by company) using HTML SELECT element with names of all companies from table companies and COUNT of products after company name in the list.

So, my goal is to get SELECT options like this:

  • Company 1 (4)
  • Company 2 (1)
  • Company 3 (2)
  • Company 4 (0)

(note: counts inside the brackets are from example above)

What have I tried so far?

I was using mysql_* functions earlier and later it was mysqli procedural model. I can do this manually with one query for companies and another one inside while block to get COUNT of elements (filtered by current company’s id in the loop). Now I’m trying to work with PDO object which is something new for me and I’m not very familiar with it.

Question

Is it somehow possible to get COUNT with one query (using JOINs or something)? If not, how I can do it with query inside the loop (old way) using my $dbPDO object? Or any other way?

I’ve looked some examples here but nothing could adapt to fit my requirements. Maybe I missed something but working with PDO is still painful for me (and I must learn it ASAP).

Looking at my own question, I don’t think it’s something hard, but the worst thing, I can’t find solution by myself.

At the end, I have solution in mysqli, just I don’t like it so much and think there’s easier way of making this task done!

I thought this question is something I need but still don’t understand that query in answer.

So far I have this code and have no idea how to make it counts products:

  $dbPDO = new PDO('mysql:dbname=comixdb;host=localhost', 'root', '');
  $sel_cat = ''; # selector HTML
  $sql = "SELECT * FROM categories ORDER BY name";
  foreach ($dbPDO->query($sql) as $row) {
    $sel_cat .= "<option value=\"{$row['id']}\">{$row['name']}</option>";
    }
  $sel_cat = "<select><option value=\"*\">All categories</option>$sel_cat</select>";

  echo $sel_cat;

Hope I’ve clarified question enough. Any help would be appreciated.

  • 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-13T12:56:26+00:00Added an answer on June 13, 2026 at 12:56 pm

    What you need can be done in SQL:

    SELECT companies.name, SUM(IF(company_id IS NULL, 0, 1)) AS products
        FROM companies
        LEFT JOIN products ON (companies.id = products.company_id)
        GROUP BY companies.id;
    

    The LEFT JOIN ensures that all companies get selected, and the SUM ensures that the count is correct (a simple COUNT would return 1 for companies with no products).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two tables in my mysql database. Table Name: groups id
I have two database tables, 1st is the TABLE_GLOBAL_PRODUCTS where all products information are
I have two tables in my database: user and media_contact . The media_contact table
I have a database framework where I have two tables. The first table has
In a MySQL database I have two tables linked in a join. One table
I have a database two tables and a linking table that I need a
I have a very simple database table that joins two other tables in a
What I have is two tables inside of a mysql database. One table contains
I Have a database called 'lms' with two tables loan and value, table loan
Basically I have a database with two tables, that is, Updates table and Images

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.