Skip to content
Advertisement

How to show/fetch amount of posts, comments etc a user has made

I want to show the users the amount of posts, comments etc they have created in their profile section, just like any other forums that exist. I am doing this with php and MYSQLI. Post table:

I have tried to add another column to the post table and increment its value by 1 each time the user makes a post in php by an INSERT statement but it’s value only stays at 1, even though the user continues to create more posts. Here is what I tried:

This is in profile.php where im trying to display the information to the user

EDITED: Thanks to ADyson the query he provided worked perfectly. I tweaked it a bit to just show the amount of posts.

Advertisement

Answer

You’ll still need to count and group if you want to see totals per user. And your join style is all wrong too – you’re supposed to link columns in each table together to make the join, not just restrict on an input field.

Try it like this:

(Obviously the WHERE clause is optional – if you want to a see a list of totals for all users then remove it.)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement