gpt4 book ai didi

php - 当一个表中的数据为空而另一个表的数据不为空时,在 PHP/MySQL 中连接两个表并显示两个表中的元素

转载 作者:行者123 更新时间:2023-11-29 02:20:54 25 4
gpt4 key购买 nike

<分区>

我有两个表,一个包含发布到博客页面的文章,第二个表包含对这些文章的评论。

我正在使用下面的函数来JOIN它们并显示来自两者的数据。

function list_articles() { 
include('core/db/db_connection.php');
$sql = "SELECT blog.title, blog.content, blog.posted_by, blog.date, article_comments.comments, article_comments.comment_by
FROM blog INNER JOIN article_comments
ON blog.content_id = article_comments.comment_id
-- WHERE blog.content != '' AND article_comments.comment_id != ''
ORDER BY blog.content_id DESC";
$result = mysqli_query($dbCon, $sql);
while ($row = mysqli_fetch_array($result)) {
echo
"<h5 class='posted_by'>Posted by " . $posted_by = $row['posted_by'] . " on " . $row['date'] . "</h5>" .
"<h1 class='content_headers'>" . $title = $row['title'] . "</h1>" .
"<article>" . $content = $row['content'] . "</article>" .
"<hr class='artline'>" .
"<div class='commented_by'>" . $row['comment_by'] . "</div>" .
"<div class='comments'>" . $row['comments'] . "</div>";
}

我遇到的问题是,每当用户插入新博客文章时,它都不会显示在网页上。如果我在另一个表(按 ID 匹配)中插入评论,则文章显示正常。即使没有相关评论,我如何显示这些文章?

我试过 -- WHERE blog.content != '' AND article_comments.comment_id = '' OR article_comments.comment_ID = '' 但它没有效果

此外,如果有更好的方法来定位这些而不是通过 ID,您能否建议一下?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com