gpt4 book ai didi

php - Select 语句没有回显数据库中的所有行

转载 作者:行者123 更新时间:2023-11-30 21:28:37 25 4
gpt4 key购买 nike

我有两个数据库表。我的 comments 表如下所示:

comid | comuserid
10 7
20 7
30 9

我的 replies 表如下所示:

repid | repcomid | repfromuser | reptouser
1 10 22 7
2 10 22 7

这是我的 index.php 页面目前的样子:

comid10 (commented by comuserid1)
[repid1] repfromuser22 to comuserid1

comid10 (commented by comuserid1)
[repid2] repfromuser22 to comuserid1

我的问题是我的代码仅回显包含回复者的 comments 行,但完全跳过不包含回复者的 comments 行。但我希望我所有的 comments 行都被回显,即使它们不包含回复者。我还希望我的回复者根据他们评论过的评论者分组在一起。这是我想要实现的真实结果:

comid10 (commented by comuserid1)
[repid1] repfromuser22 to comuserid1
[repid2] repfromuser22 to comuserid1

comid20 (commented by comuserid1)

comid30 (commented by comuserid2)

我如何用我当前的代码实现它?请帮助:

<?php
$rqid = (int)$_GET['rqid'];
$query = $conn->query("
SELECT comments.*, replies.*, users.username
FROM comments
INNER JOIN replies
ON comments.comid = replies.repcomid
INNER JOIN users
ON comments.comid = users.userid
WHERE comments.comrqid = {$rqid}
");
$comreps = [];
while($row = $query->fetch_object()) {
$comreps[] = $row;
}
?>

<?php foreach($comreps as $comrep): ?>
<div class="rqdivc">
<?php echo $comrep->username; ?>
</div>
<div class="fxrp">
<div>
<?php echo $comrep->repfromuser; ?> -->
<?php echo $comrep->reptouser; ?>
</div>
</div>
<?php endforeach; ?>

最佳答案

Strawberry 建议 - 使用 LEFT JOIN - 解决了我的问题。谢谢你!

关于php - Select 语句没有回显数据库中的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57489167/

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