我不知道我是否丢失了大脑中的信息,或者我是否从未使用过,但我创建了一条推文,以便用户可以关注其他用户。
现在的问题是每个用户都显示相同的信息。
我试图将WHERE $_SESSION['uid']
添加到我的查询中,但这并没有帮助我们做到这一点。
仍显示相同的信息。 有什么提示吗?
<?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;
}
?>
答案 0 :(得分:1)
如果没有WHERE
子句,SQL
将获取所有用户的详细信息
另外,我认为JOIN
上的tweets
&amp; following
应该是LEFT JOIN
(即使没有records
也可以获得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
)
{{1}}