vendredi 31 juillet 2015

SELECT from table join

I have 3 tables:

users
  user_id
  first_name
forum_post
 post_id
 post_title
user_post_join
  id
  user_id
  post_id

The join table takes the user_id from users and post_id from forum_posts.

What I am trying to display is a list of posts from the forum_post table and the user who posted it. I am struggling to know where the join should be, here is my attempt so far...

function build_forum_posts(){

        global $dbc;

        $q = "SELECT users.user_id, forum_post.post_id, user_post_join.id 
            FROM users 
            INNER JOIN user_post_join ON users.user_id = forum_posts.post_id
             ";  

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

        // FETCH AND PRINT ALL THE RECORDS
        while ($row = mysqli_fetch_array($r)) {

        echo '
        <div class="post">
            <div class="col-group-2">
                <h3>'.$row["post_id"]. '</h3>
                <p>By: '.$row["user_id"]. '</p>
            </div>
            <div class="col-group-2">
                <div class="post_count">
                    <h3  class="answer">0</h3>
                    <p class="answer">Answers</p>
                </div>
            </div>  
        </div>
        ';

        } 

Aucun commentaire:

Enregistrer un commentaire