0

I'm trying to get a list of usernames from my following table, and then use that list to get each user's post for a news feed, but when I try this:

    $sth = $db->query("SELECT profile FROM follows WHERE follower='$username' ");
    $following = $sth->fetchAll(PDO::FETCH_ASSOC);

    foreach ($following as $userlist) {
        $userlist_logic .= "username='$userlist' OR ";
    }

echo $userlist_logic;

Then it just returns this:

username='Array' OR username='Array' OR username='Array' OR

How do I fix it?

user2250471
  • 1,002
  • 3
  • 10
  • 23

2 Answers2

2

$userlist is an array instance containing the profile column for each row returned.

The line within your foreach loop needs to be as follows:

$userlist_logic .= "username='{$userlist['profile']}' OR ";

Also, I would recommend sanitising the variable before interpolating it into the string by generating your second SQL statement within PDO and then running it through PDO prepare as detailed in this StackOverflow thread.

Community
  • 1
  • 1
Benjamin Nolan
  • 1,170
  • 1
  • 10
  • 20
0

In the loop do:

foreach ($following as $userlist) {
   $userlist_logic[] = "username='{$userlist['profile']}'";
}

Then just implode the array you created to create a list of that joined with OR:

$userlist_logic = implode(' OR ', $userlist_logic);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87