Php mysql中来自多个表的嵌套foreach循环查询存在问题

Php mysql中来自多个表的嵌套foreach循环查询存在问题,php,mysql,foreach,nested,Php,Mysql,Foreach,Nested,我刚刚开始学习php和mysql。我的数据库中有两个查询。首先查询一个名为lifestyles的表,然后执行foreach循环并输出行中的每个项。现在,这些行中的每一行都有评论框,供成员发表评论。comments表中的行具有名为postid的关联列。因此,我在前一个循环中做了另一个foreach循环,在每行生活方式下面显示这些注释。 问题在于,每个评论都会被复制到每个生活方式查询的次数。 见下文: Title: TEST Date: 28th-Jun-2011 at 10:10 PM

我刚刚开始学习php和mysql。我的数据库中有两个查询。首先查询一个名为lifestyles的表,然后执行foreach循环并输出行中的每个项。现在,这些行中的每一行都有评论框,供成员发表评论。comments表中的行具有名为postid的关联列。因此,我在前一个循环中做了另一个foreach循环,在每行生活方式下面显示这些注释。 问题在于,每个评论都会被复制到每个生活方式查询的次数。 见下文:

Title:  TEST

Date:   28th-Jun-2011 at 10:10 PM

TEST LIFESTYLE POST

Comments

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT
和代码:

<?php

        $lifestyles=dbAll("select * from user_accounts, lifestyle where lifestyle.category=1 and lifestyle.user_id=user_accounts.id order by cdate DESC");

        if(!$lifestyles){
            echo '<em><strong>No Posts yet. Why don\'t you be the first!</strong></em>';
        }
        else{

            foreach($lifestyles as $lifestyle){
                $lifestyleid=(int)$lifestyle['id'];
                echo '<form class="postform" method="post" action="shopping.php?redirect='.$_SERVER["PHP_SELF"].'">';
                echo '<input type="hidden" name="postshop" value="1" />';
                echo '<input type="hidden" name="postid" value="'.$lifestyleid.'" />';
                echo '<table class="postinfo"><tr><th>Title:</th><td style="font-weight:bold">'.$lifestyle['title'].'</td>
                </tr>';
                echo '<th>Date:</th><td>'.date('dS-M-Y', strtotime($lifestyle['cdate'])).' at'.' '.date('g:h A', strtotime($lifestyle['cdate'])).'</td>
                <th>By:</th><td>'.$lifestyle['firstname'].' '.$lifestyle['lastname'].'</td>';
                echo '</table>';

                echo '<div class="postbody"><table>';
                echo '<tr><td>'.$lifestyle['body'].'</td><td></td></tr>'.'</table></div>';

                $comments=dbAll("select * from comments,lifestyle where comments.postid=$lifestyleid and section_id=1 order by commentdate DESC");

                echo '<div class="comment">';
                echo '<table><tr><th>Comments</th></tr>';

                **foreach($comments as $comment)
                {
                    echo '<tr><td>'.$comment['commentbody'].'</td></tr>';
                }**

                echo '<tr><td><textarea rows="1" title="Write a comment" name="comment" placeholder="Write a comment..."></textarea></td></tr>';
                echo '</table></div>';
                echo '<input type="submit" name="action" value="Save Comment" />';
                echo '<hr />';
                echo '</form>';
            }

        }
    ?>

问题在于这一行的查询:

$comments=dbAll("select * from comments,lifestyle where comments.postid=$lifestyleid and section_id=1 order by commentdate DESC");
您没有将注释表加入生活方式表。 尝试添加联接,将查询更改为:

select *
from comments,lifestyle
where comments.postid=$lifestyleid
and section_id=1
---->>
AND comments.postid = lifestyle.id
order by commentdate DESC

您不应该在这样的循环中嵌套相关查询。连接到数据库、发送查询、生成结果集并将该结果集发送回应用程序的成本很高。做一次,而不是像你循环的次数那样多。您可以在一个查询中连接所有3个表,一次获得“生活方式”和所有相关的“注释”。按生活方式排序,然后按评论日期排序,在应用程序端,每组评论只打印一次生活方式。非常感谢。我添加了连接,所有的工作都像魔术一样。但我将修改查询以同时联接所有3个表。再次感谢各位。如果这个答案对你们有帮助,别忘了接受它。这有助于我们双方的声誉。