php - Using COUNT in a MYSQL Statement -
users can posts , posts can have comments , want number of comments made in users posts.
posts table:
id | content | foto | likes | genious | user_id | timepost | youvideo |
comments table:
id | users_id | posts_id | content | timecomment |
$stmt = $mysqli->prepare("select count(comments.id) comments inner join posts on posts.id=comments.users_id posts.user_id=? "); $stmt->bind_param('i',$_session['id']); $stmt->execute(); $stmt->bind_result($comentarios); $stmt->fetch(); $_session['comentarios']=$comentarios;
i suppose, have post_id
foreign key in comments table.
if want comments, made on users posts, try this:
select count(comments.id) comments inner join `posts` on comments.post_id=posts.id , `posts`.user_id=?
Comments
Post a Comment