如何从我关注的人和php和mysql中的帖子中获取帖子?

I have table

  1. answers(aid,user_id,...) - posts table

  2. users (userid,name,...)

  3. followers(user_one,user_two) - where user_one is the follower and user_two is the followed by person.

I need to show posts from people I follow and my posts using php and mysql in my home feed.But currently it is not working as expected.Each posts is showing 7 times.

curent select query

<?php

    $my_id = $_SESSION['user_id'];
     $sql_query = "SELECT * FROM answers left join users on users.userid = answers.user_id LEFT join followers on followers.user_one = answers.user_id WHERE answers.user_id= '$my_id' ORDER BY answers.date_created DESC";  
?>

Didn't see you also want to return your posts. Try below sql:

     select * from answers, users, (select user_one user_id from followers where user_two='$my_id' union select '$my_id' user_id) a where answers.user_id=users.user_id and answers.user_id=a.user_id;

There is error in your sql: SELECT * FROM answers left join users on users.userid = answers.user_id LEFT join followers on followers.user_one = answers.user_id WHERE answers.user_id= '$my_id' ORDER BY answers.date_created DESC".

the second left join should be followers.user_two=ansers.user_id, and the where statement should be followers.user_one='$my_id';

One option here is to phrase your query as a union of two queries. The first half of the union below finds all posts given by people you follow. The second half of the union finds all of your posts.

SELECT a.*
FROM answers a
INNER JOIN followers f
    ON (a.user_id = f.user_two AND f.user_one = $my_id)
UNION ALL
SELECT a.* FROM answers WHERE user_id = $my_id;

I'm not a PHP person, but hopefully you can easily adapt the above query into your code.