Display number of comments from a single user table

Asked

Viewed 36 times

-2

How to count comment lines in the comment table that have been added by a particular user.

the table is this

id |post_body | posted_by | posted_to | date_added |removed   | post_id
1  |teste 1   |  joao     | maria     | 10-10-10   | no     | 23
2  |teste 2   |  carlos   | joao      | 10-10-10   | no     | 24
3  |teste 3   |  joao     | carlos    | 11-10-10   | no     | 25
4  |teste 4   |  joao     | maria     | 12-10-10   | no     | 26
4  |teste 4   |  maria    | joao      | 12-10-10   | no     | 27

I want to display the comment number only of john.
ex: total comments 3

2 answers

0


Select COUNT(ID) from usuario WHERE posted_by = 'joao'

0

Use the following:

<?php 
$query = mysqli_query($conn, "SELECT COUNT(*) as total FROM tabela_comentarios WHERE posted_by = 'joao';");
$row = mysqli_fetch_array($query);

echo "Joao comentou: ".$row['total']." vezes!";
?>

NOTE: I didn’t test the code. But I believe it works. Hugs!

  • It worked perfectly. Thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.