0

I dont know if I lost the information in my brain, or if I never had it, but I have created a tweet so a user can follow other users.

The problem now is that every user display the same information. I Was trying to add WHERE $_SESSION['uid'] to my query but that doesn't help do the trick.

Still shows the same info. Any tips ?

<?php 
    foreach(fetch_follotweets() as $tweet){ 
        echo $tweet['firstname']; 
        echo $tweet['lastname'];
        echo $tweet['username'];
        echo $tweet['date'];
        echo $tweet['profile_img'];
        $tweet['message'];
    }

    function fetch_follotweets(){
        global $db;
        $query = $db->query("   SELECT
                                  user.email,
                                  user.username,
                                  tweets.message,
                                  tweets.date,
                                  userdetails.profile_img,
                                  userdetails.firstname,
                                  userdetails.lastname,
                                  following.id,
                                  following.user_id,
                                  following.follow_id
                                FROM user
                                  JOIN userdetails
                                    ON user.id = userdetails.user_id
                                  JOIN tweets
                                    ON userdetails.user_id = tweets.user_id
                                  JOIN following
                                    ON following.follow_id
                                WHERE following.follow_id = tweets.user_id AND
                                user.id='{$_SESSION['uid']}
                                ORDER BY tweets.date DESC");
            $tweet = array();
        while(($row = $query->fetch(PDO::FETCH_ASSOC)) !==FALSE) { 
            $tweet[] = $row;
        }
        return $tweet;
    }
    ?>
4

1 回答 1

1

Without the WHERE clause, the SQL would get the details of all the users

Also, I think that the JOIN on tweets & following should be a LEFT JOIN ( to get the records even if none of them is present )

SELECT
  user.email,
  user.username,
  tweets.message,
  tweets.date,
  userdetails.profile_img,
  userdetails.firstname,
  userdetails.lastname,
  t.ids_of_users_followed
FROM 
  user
  JOIN userdetails
    ON user.id = userdetails.user_id
  LEFT JOIN (
      SELECT
        GROUP_CONCAT( following.follow_id ) AS ids_of_users_followed
        following.user_id
      FROM
        following
      GROUP BY
        following.user_id
    ) as t ON ( t.user_id = user.id )
  JOIN tweets
    ON ( userdetails.user_id = tweets.user_id OR FIND_IN_SET( tweets.user_id, t.ids_of_users_followed ) )
WHERE 
  user.id = :?    => Make sure to add the logged in user_id here
ORDER BY 
  tweets.date DESC
于 2013-04-19T19:09:40.467 回答