mysql - Get number of posts in a topic PHP -


How do I display the number of posts on a topic like a forum? I used it ... (how much naBocha):

  function numberfost ($ n) {$ sql = "SELECT * FROM posts WHERE topic_id = '". $ N "'"; $ Result = mysql_query ($ sql) or die (mysql_error ()); $ Count = mysql_num_rows ($ result); Echo number_format ($ count); }  

Loop of listing topics:

  & lt ;? Php $ sql = "SELECT *" topics subject to ORDER subject_id ASC limit $ $ starting, $ range "; $ Result = mysql_query ($ sql) or die (mysql_error ()); While ($ line = mysql_fetch_array ($ result)) {? & Gt; & Lt; Div class = "topic" & gt; & Lt; Div class = "theme-name" & gt; & Lt; P & gt; & Lt ;? Php echo $ row ['topic_title']; ? & Gt; & Lt; / P & gt; & Lt; / Div & gt; & Lt; Div class = "subject-post" & gt; & Lt; P & gt; & Lt ;? Php Echo NumberFopost ($ line ['topic_id']); ? & Gt; & Lt; / P & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt ;? Php}? & Gt;  

Although this is a bad way of doing this ... I just need to know what would be the best way, do not just give me a website, do it here because Am I trying to know more precisely?

Edit:

SQL:

Thanks.

  create tab 'post' (`post_id` middle finger (8) tap autacncrment, 'subject_id' secondary (8) no tap,` forum_id secondary '(8) Tap, `user_id` moderate (8) no tap, 'post_time' code (100) tap,` post_times_stamp` moderate (20) voice, 'post_ip' code (20) voice, 'post_parted' tint (1) tap, `post_reportdesc `Text tap, primary key (` post_id`) engine = mice default charset = latin1 aUTO_INCREMENT = 2; - - INSERT (1, 1, 0, 1, '15 Juni', 0, '', 0, '') for the values ​​of data dumping -`positions for table 'posts'; Subject Table:  
  Make Table 'Topic' (`Subject_Aud 'Secondary (8) Faucet AUTONCRment,` Section_Name` code (25) Noel, `Subject_title' varchar (120) No tap,` subject_details Do not tap 'four' (120), 'user_id' secondary (8) no tap, `theme_time 'varchar (100) no tap,` topic_view' varchar (1000) tap, `subject_p` secondary (11) no tap,` subject_productive` Medium (11) no tap, subject_stats tint (1) tap, primary key (`subject_id`)) engine = mice default charset = latin1 AUTO_INCREMENT = 1;  

It should help you understand a bit more.

You can use:

  "SELECT COUNT (topic_id ) By WHERE topic_id = '?' " 

What? Is a placeholder if you use mysql, then you use mysql_real_escape_string :

  $ sql = "SELECT COUNT (topic_id) WHERE topic_id = '" should do. Mysql_real_escape_string ($ n) "'";  

If you use mysql_fetch_array , then the $ line [0] will count you can name it but it is necessary is not.

A better option is a few ready-made statements, such as whether it helps in preventing SQL injection.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -