title: Is it possible to echo multiple results outside of a while loop?
body: <p>I have a while loop that I use to echo multiple mysql results that translate into offline users. To make things more organized, I wanted to perform the query, store the results in a variable and then echo it at the bottom of the page. I need to echo both online and offline users within a certain parent div, so it seems cluttered to me to echo the first div tag and then perform both queries and echo the results, then echo the closing tag. Currently, if I try to echo the result from outside the while loop, I only get 1 result. If anyone has any ideas, I would appreciate it.</p>
  
  <pre><code> $sql = 'SELECT * FROM users
  LEFT JOIN friendships
  ON friendships.friend_id = users.id
  WHERE friendships.user_id = ?
  AND users.id NOT IN (
    SELECT active_users.id FROM active_users)';
  $stmt5 = $conn-&gt;prepare($sql);
  $result=$stmt5-&gt;execute(array($userid));
  
      while ($row = $stmt5-&gt;fetch(PDO::FETCH_ASSOC)) {
      $online=htmlspecialchars( $row['username'], ENT_NOQUOTES, 'UTF-8' );
  
  $online = "&lt;div class='user-online'&gt;
      &lt;a data-name=\"$to\"&gt;$to&lt;/a&gt;
  &lt;/div&gt;";
  $online.=etc...plus do other processes
  }
  
      &lt;div id=\"online\"&gt;
          ".$online."
      &lt;/div&gt;
  </code></pre>