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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:15:56+00:00 2026-05-26T19:15:56+00:00

I have two tables in my database. cat – catid, catname articles – id,

  • 0

I have two tables in my database.

cat – catid, catname
articles – id, catid, content

so what i want to display is category name (catname) and how many articles are there in that category.

This is my code but it dosent work.

$query = "SELECT cat.cname,  COUNT(articles.cat_id)".
"FROM cat, articles ".
    "GROUP BY cat_id";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    echo $row['cname']. " - ". $row['COUNT(cat_id)'];
    echo "<br />";
}

Any help will be most aprriceated. thanks.

  • 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-26T19:15:56+00:00Added an answer on May 26, 2026 at 7:15 pm

    So this is a 1:many relationship. i.e. 1 Category -> Many articles.

    The best way to do this is to create a third table, an adjacency list.

    Keep your ‘category_id’ and ‘article_id’ unique in tables ‘cat’ and ‘article’.

    In your third table you define the 1 : many relationships.

    Table 3: cat_articles
    
    adj_id    cat_id    art_id
    1         1         1
    2         1         2
    3         2         3
    4         2         4
    5         3         5
    

    Now join the tables:

    $sql = "SELECT * FROM `cat_articles` adj ".
           "LEFT JOIN (`cat` cat, `articles` art)".
           "ON (cat.cat_id = adj.cat_id AND art.art_id = adj.art_id) ";
    

    This takes the adjacency table, preserves it’s format(due to left join) and appends the tables article and category to it, giving you a categorised table of all your articles. You can now use mysql_fetch_array() to get your results.

    Edit: reference first comment, displaying number of rows

    You can either, as you have done, use SQL’s function COUNT to return a count of a specific column.

    Or, with PHP, run the query, and then use mysql_num_rows($result) to return the number of rows SQL has in its buffer.

    Alternatively, retrieve results using mysql_fetch_array($result) and use count to return the number of paired values in the array.

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

Sidebar

Related Questions

I have two database tables that have a many to many relationship: People and
I have two tables in my database: Products id (int, primary key) name (varchar)
I have two database tables with a one-to-many relationship. Is it possible to select
I have two tables in the database Team:Id(int PK), Name(nvarchar) and Player:Id(int PK), Name(nvarchar),
I have two database tables Contact (Id, Name, ...) and ContactOperationalPlaces (ContactId, MunicipalityId), where
I have two tables in my database - Category and Department which both contain
I have two tables in database: A: [ ID, name, SECRET_KEY] examplary rows: ID1,
I have two database tables (lets call them A and B) with many-to-many bridge
I have two database tables with the following structure: actions: action_id int(11) primary key
I have two tables in my database, called ratings and movies . Ratings: |

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.