从users表中获取用户信息

时间:2015-08-04 13:01:04

标签: php mysql

我有一个查询集,我在这里输出绑定到帖子的评论,并显示通过此查询发布评论的用户:

$q = "SELECT p.post_id, f.created_date, f.comment_id, f.comment_content, j.id, u.user_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 u ON u.user_id = u.user_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;">By: <a href="user_view.php?id=' . $row["user_id"] . '">'.$row["user_id"] . '</a> ' .date("F j, Y, g:i a", strtotime($row["created_date"])). '</p>
</div>
';

}  

正如您所看到的那样,最终结果会输出注释以及user_id,但是如果我以u.first_name为例,则无法输出结果。在我的上面,它将输出谁发布但很难获得他们名字的用户ID。

我有这些表格:

users
  user_id
  first_name
  last_name

comment_post_join
  comment_id (Fk)
  post_id (Fk)

user_comment_join
  user_id (Fk)
  comment_id (Fk)

user_post_join
  user_id (Fk)
  post_id (Fk)

forum_post
  post_id
  post_title
  post_content
  post_created
  post_topic

forum_comment
  comment_id
  comment_content
  created_date

更新

 function build_post_view(){

        global $dbc;

        $id = $_GET['post_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'
             ";   

        $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;">
            <h3>'.$row["post_title"]. '</h3>
            <p style="font-size: 13px;">By: <a href="user_view.php?id=' . $row["user_id"] . '">'.$row["first_name"] . ' ' .$row["last_name"]. '</a> ' .date("F j, Y, g:i a", strtotime($row["post_created"])). '</p>
            <br>
            <p style="font-weight: 500; line-height: 150%;">' . $row["post_content"] . '</p>
            <br><br>
        </div>    
        ';

        } 


$q = "SELECT p.post_id, f.created_date, f.comment_id, f.comment_content, j.id, u.user_id, u.first_name, u.last_name
         FROM forum_post AS p
         INNER JOIN users AS us ON us.user_id = u.user_id
         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 u ON u.user_id = u.user_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_assoc($r)) {

echo '
<div style="border-bottom: thin solid #ddd;">
    <p style="font-size: 13px;">'.$row["comment_content"] . '</p>
    <p style="font-size: 12px;">By: <a href="user_view.php?id=' . $row["user_id"] . '">'.$row["user_id"] . ' '.$row["first_name"] . ' '.$row["last_name"] . '</a> ' .date("F j, Y, g:i a", strtotime($row["created_date"])). '</p>
</div>
';

}   
}

2 个答案:

答案 0 :(得分:1)

更新#13

    $q = "SELECT p.post_id, fc.created_date, fc.comment_id, fc.comment_content, u.user_id, u.first_name, u.last_name
         FROM forum_post AS p
         INNER JOIN comment_post_join AS cp ON cp.post_id = p.post_id
         INNER JOIN forum_comment AS fc ON fc.comment_id = cp.comment_id
         INNER JOIN user_comment_join AS uc ON uc.comment_id = fc.comment_id
         INNER JOIN users AS u ON u.user_id = uc.user_id
         WHERE p.post_id = '$id'
         ORDER BY fc.created_date ASC
     ";   

if ($r = mysqli_query($dbc, $q)) { // Run the query.

    // FETCH AND PRINT ALL THE RECORDS
    while ($row = mysqli_fetch_assoc($r)) {
    echo '
    <div style="border-bottom: thin solid #ddd;">
        <p style="font-size: 13px;">'.$row["comment_content"] . '</p>
        <p style="font-size: 12px;">By: <a href="user_view.php?id=' . $row["user_id"] . '">'.$row["first_name"] . ' '.$row["last_name"] . '</a> ' .date("F j, Y, g:i a", strtotime($row["created_date"])). '</p>
    </div>
    ';
    }

}else{
    echo 'ERROR: ' . mysqli_error($dbc) . '';
}

答案 1 :(得分:0)

试试这个;

SELECT 
    p.post_id, f.created_date, f.comment_id, f.comment_content, j.id, u.user_id
FROM 
    forum_post p,forum_comment f,user_comment_join ux,comment_post_join j,users u
WHERE
    p.post_id = j.post_id AND
    f.comment_id = j.comment_id AND
    ux.post_id  = j.post_id AND
    u.user_id = ux.user.id AND
    p.post_id = '$id'
ORDER BY created_date ASC