【发布时间】:2019-06-28 11:34:16
【问题描述】:
我的查询生成两个结果,例如
然后我尝试使用两个带有两个 while 循环的数组来提取结果
while ($rowPost = mysqli_fetch_assoc($grabPostsQuery)) {
$posts[] = $rowPost;
}
while ($rowComment = mysqli_fetch_assoc($grabCommentsQuery)) {
$comments[] = $rowComment;
}
$grabPostsQuery = "SELECT * FROM posts ORDER BY postDate DESC";
$grabPostsQuery = mysqli_query($link, $grabPostsQuery);
$grabCommentsQuery = "SELECT * FROM comments ORDER BY commentDate DESC";
$grabCommentsQuery = mysqli_query($link, $grabCommentsQuery);
然后我做了一个嵌套的foreach,如下所示
foreach($posts as $post) {
foreach($comments as $comment) {
echo $post['postContent']."<br>";
echo $comment['commentContent']."<br>";
}
}
我可以同时访问帖子和 cmets,唯一的问题是 foreach 循环在帖子和 cmets 中循环多次,然后显示两次
有什么帮助吗?
【问题讨论】:
-
为什么不使用 JOIN?
-
如何加入显示结果是不合时宜的,因为排序不符合预期
-
在这里使用 JOIN ..
-
我不理解不合时宜的意思。听起来像特朗普主义(或丛林主义)
-
我希望能回答您的问题,感谢您的接受,点赞也不错:D