I'm working on a blogging system website. In this blogging system, I have two tables. One is called `blogs` which simply contains the information of a blog (such as *blog\_title*, *blog\_author*, *blog\_category*). 

And the other one is called `categories` which only contains the *blog\_category* names of blog posts. 

Now I have made a page where users can see the blog categories and **the number of blog posts within that custom category name**.

So here is how it looks like:

[![print screen](https://i.stack.imgur.com/ijn50.png)](https://i.stack.imgur.com/ijn50.png)

And this is the code behind of that:

```
foreach($catShow as $cat){ 
echo "
    <tr>
        <td>".$cat['table_id']."</td>
        <td>".$cat['cat_title']."</td>
        <td>".$cat['cat_id']."</td>
        <td>".num_cats($cat['cat_id'])."</td>
        <td></td>
    </tr>";
}

```

So the function `num_cats()` basically counts the number of blog posts at `blogs` table which has the same blog category id (`cat_id`):

```
function num_cats($id){
        $num_cats = "SELECT COUNT(*) FROM blogs WHERE blog_category = '$id'";
        $run_num = mysqli_query($GLOBALS['con2'],$num_cats);
        $return = '';
        if (!$run_num) {
            die(mysqli_error($GLOBALS['con2']));
        }
        $numCat = mysqli_num_rows($run_num);
        $return .= " 
            $numCat
        ";
        return $return;
}

```

But now the problem is, the result is incorrect. I mean the table only shows **1** result for each category however some of them have more than one item at the `blogs` table. 

So what is wrong with this code, can you please help me with that!