Hi I was wonder what is the correct way of selecting the number of comments from a post and selecting who made the original post (this is for a news script):
news_posts structure
- id
- uid
$query = mysql_query("SELECT * FROM news_posts ORDER BY dateline DESC");
while($post = mysql_fetch_array($query)) {
// Selecting the member username
$memberquery = mysql_query("SELECT username FROM news_members WHERE id='$post[uid]'");
$username = mysql_result($memberquery, 0);
// Selecting the number of comments
$commentquery = mysql_query("SELECT COUNT(*) FROM news_comments WHERE pid='$post[id]'");
$comments = mysql_result($commentquery, 0);
...
}
or
news_posts structure:
- id
- username // This is the username not the uid
- comments // This is a counter for number of comments and is updated when a new post is made
$query = mysql_query("SELECT * FROM news_posts ORDER BY dateline DESC");
while($post = mysql_fetch_array($query)) {
// Selecting the member username
$username = post['username'];
// Selecting the number of comments
$comments = $post['comments'];
...
}
The second one reduces alot of queries if you're selecting alot of posts but also adds somewhat un needed fields to the table. If you could help I'd be very greatful. I'd also appreciate any information why one would be better than the other. Thanks
news_posts structure
- id
- uid
$query = mysql_query("SELECT * FROM news_posts ORDER BY dateline DESC");
while($post = mysql_fetch_array($query)) {
// Selecting the member username
$memberquery = mysql_query("SELECT username FROM news_members WHERE id='$post[uid]'");
$username = mysql_result($memberquery, 0);
// Selecting the number of comments
$commentquery = mysql_query("SELECT COUNT(*) FROM news_comments WHERE pid='$post[id]'");
$comments = mysql_result($commentquery, 0);
...
}
or
news_posts structure:
- id
- username // This is the username not the uid
- comments // This is a counter for number of comments and is updated when a new post is made
$query = mysql_query("SELECT * FROM news_posts ORDER BY dateline DESC");
while($post = mysql_fetch_array($query)) {
// Selecting the member username
$username = post['username'];
// Selecting the number of comments
$comments = $post['comments'];
...
}
The second one reduces alot of queries if you're selecting alot of posts but also adds somewhat un needed fields to the table. If you could help I'd be very greatful. I'd also appreciate any information why one would be better than the other. Thanks