获取报销我正在评论系统的显示帖子,评论(家长评论)和评论repliess(孩子评论)的地方。在评论系统
MySql的意见表看起来如下:
comment_id int primary_key
post_id int // where i save the post id
author varchar
comment text
parent_id int // where i save parent comment id in case this comment is reply of another comment
现在,我的情况是,当我经过的MySQL结果集打印评论信息。正好在试图打印子注释(其他注释的评论)和父注释(注释的帖子)时。
我目前的方法来实现这一目标,是选择使用PHP有没有PARENT_ID
SELECT * FROM comments WHERE parent_id = null
而遍历所有结果的所有意见,然后再进行循环的,如果有任何的孩子每个注释搜索内另一查询它的评论。
<?php
$sql_result_set;
while($comment = mysql_fetch_array($sql_result_set, MYSQL_ASSOC){
// process comment info
$sql = "SELECT * FROM comments where parent_id = ".$comment['comment_id'];
$result = mysqli_query($sql);
if(mysql_num_rows($result)>0){
while($child_comments = mysql_fetch_array($result, MYSQL_ASSOC)){
// child comments of the parent
}
}
}
?>
现在,我的问题是“有没有更好的方式做同样的事情?”
您的帮助表示赞赏
一切顺利,
这将有效地s按照特定的顺序从'comments'表中选择所有记录。这是你真正想做的吗? – DaveRandom
你可能想看看这个问题:http://stackoverflow.com/q/937042/1627227。这是一种稍微不同的方法,但比每个评论回复有一个新的数据库查询更有效。 – martinczerwi
@DaveRandom不,他们不是所有的生产将返回限制数量的评论 –