I have a table called noticias
, where every news item has id
. I also have a table named comentarios
, where there is id
of the news that the user commented on. How can I tell the comments for that news?
I have a table called noticias
, where every news item has id
. I also have a table named comentarios
, where there is id
of the news that the user commented on. How can I tell the comments for that news?
You can do as follows, it is good to add an alias (name) to the count so it is easy to remember the field name in php.
$contador = mysql_query("SELECT count(id) as total FROM 10cms_noticias_resp
WHERE id_noticia = id_noticia") or die(mysql_error());
$comentario = mysql_fetch_assoc($contador);
echo $comentario['total'];
A simple query by id, already solves:
SELECT COUNT(*) AS total_comentarios FROM comentarios where id_noticia=:id