gpt4 book ai didi

php - 使用表连接选择特定数据

转载 作者:行者123 更新时间:2023-11-30 22:04:55 25 4
gpt4 key购买 nike

我试图通过从问题表中加入它来从用户表中输出一些用户用户名,目的是我可以显示哪个用户发布了这个特定问题。

具有id用户名的用户

discussion_q id, question_text, user_id

这里是我所在的地方:

$sql = "SELECT q.id AS questionId, q.question_text AS questionText, q.user_id AS questionUserId, q.published AS questionPub, users.id AS userId 
FROM discussion_q
JOIN users
ON questionUserId = userId
WHERE project_id = '$projectId'
ORDER BY published";

当然,我得到 0 个返回给我的结果。我确定我过度设计了这个还是错过了一些简单的东西?

这是我的 php 返回结果:

    $result = $conn->query($sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<div class="twelve columns">
<p><a href=".php?project_id=' . $row['id'] .'">' . $row['question_text'] . '</a></p>
<p>' . $row['published'] . ' by ' . $row['username'] . '</p>
</div>';
}
} else {
echo "0 results";
}

所以最终目标是输出带有发帖用户用户名的问题文本。

最佳答案

$sql = "SELECT q.id AS questionId, q.question_text AS questionText, q.user_id AS questionUserId, q.published AS questionPub, users.id AS userId 
FROM discussion AS q
JOIN users
ON (q.user_id = users.id)
WHERE project_id = '$projectId'
ORDER BY published";

关于php - 使用表连接选择特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42123536/

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