How to count number of records in a table?

4

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?

    
asked by anonymous 25.07.2015 / 23:08

2 answers

1

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'];
    
26.07.2015 / 00:32
1

A simple query by id, already solves:

SELECT COUNT(*) AS total_comentarios FROM comentarios where id_noticia=:id
    
09.12.2015 / 16:40