我目前有一个查询集,用于选择与特定帖子相关联的评论。我还需要选择针对该帖子提交评论的用户详细信息。我在这里苦苦挣扎,需要一些建议。
这是我当前的查询:
$q = "SELECT p.post_id, f.created_date, f.comment_id, f.comment_content, j.id
FROM forum_post AS p
INNER JOIN comment_post_join AS j ON p.post_id = j.post_id
INNER JOIN forum_comment AS f ON f.comment_id = j.comment_id
WHERE p.post_id = '$id'
ORDER BY created_date ASC
";
$r = mysqli_query ($dbc, $q); // Run the query.
// FETCH AND PRINT ALL THE RECORDS
while ($row = mysqli_fetch_array($r)) {
echo '
<div style="border-bottom: thin solid #ddd;">
<p style="font-size: 13px;">'.$row["comment_content"] . '</p><p style="font-size: 12px;">' .date("F j, Y, g:i a", strtotime($row["created_date"])). '</p>
</div>
';
}
正如您所看到的,我正在从forum_post
表中选择,因为这是我的帖子所在的位置。我使用inner_join
来引用帖子及其相关评论,但我还需要从用户那里获取详细信息,因为我还有另一个表user_comment_join
,在提交评论时也会更新创建行的表,并提交用户ID和注释ID。
我对实际帖子做了类似的事情并通过这样做引用用户:
$q = "SELECT u.user_id, u.first_name, u.last_name, f.post_created, f.post_id, f.post_title, f.post_content, j.id
FROM users AS u
INNER JOIN user_post_join AS j ON u.user_id = j.user_id
INNER JOIN forum_post AS f ON f.post_id = j.post_id
WHERE f.post_id = '$id'
";
我希望我在这里解释了我的问题,任何建议都会很棒。
我自己也是这样尝试过......:
$q = "SELECT p.post_id, f.created_date, f.comment_id, f.comment_content, u.user_id, u.first_name, u.last_name, j.id
FROM forum_post AS p
INNER JOIN comment_post_join AS j ON p.post_id = j.post_id
INNER JOIN forum_comment AS f ON f.comment_id = j.comment_id
INNER JOIN user_comment_join AS cj ON u.user_id = cj.user_id
WHERE p.post_id = '$id'
ORDER BY created_date ASC
";
但是我收到以下错误:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\MAMP\htdocs\functions\functions.inc.php on line 963
反过来又指这一行...:
while ($row = mysqli_fetch_array($r)) {
如果我在phpmyadmin中运行此查询,我会得到以下内容:
Unknown column 'u.user_id' in 'field list'
答案 0 :(得分:1)
你忘了加入users
表。试试这个:
$q = "SELECT p.post_id, f.created_date, f.comment_id, f.comment_content, u.user_id, u.first_name, u.last_name, j.id
FROM forum_post AS p
INNER JOIN comment_post_join AS j ON p.post_id = j.post_id
INNER JOIN users AS u ON u.user_id = j.user_id
INNER JOIN forum_comment AS f ON f.comment_id = j.comment_id
INNER JOIN user_comment_join AS cj ON u.user_id = cj.user_id
WHERE p.post_id = '$id'
ORDER BY created_date ASC
";