Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could try something like below:</p> <pre><code>/* Get the 10 latest posts from users you're following */ $stmt = $cxn-&gt;prepare('SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ?) ORDER BY datetime DESC LIMIT 15'); $stmt-&gt;bind_param('i', $user_id); $stmt-&gt;execute(); $result = $stmt-&gt;get_result(); /* If a result exists, continue. */ if ($result-&gt;num_rows) { while ($row = $result-&gt;fetch_assoc()) { /* Get the user's username from their id */ $stmt = $cxn-&gt;prepare('SELECT username FROM users WHERE user_id = ?'); $userid = (int)$row['user_id']; $stmt-&gt;bind_param('i', $userid); // Forcing $row['user_id'] to int $stmt-&gt;execute(); $stmt-&gt;bind_result($username); $stmt-&gt;fetch(); //$stmt-&gt;close(); Don't close it here } } $stmt-&gt;close(); </code></pre> <p>Another approach but little more time consuming is: </p> <pre><code> /* Get the 10 latest posts from users you're following */ $stmt = $cxn-&gt;prepare('SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ?) ORDER BY datetime DESC LIMIT 15'); $stmt-&gt;bind_param('i', $user_id); $stmt-&gt;execute(); $result = $stmt-&gt;get_result(); /* If a result exists, continue. */ if ($result-&gt;num_rows) { while ($row = $result-&gt;fetch_assoc()) { /* Get the user's username from their id */ $stmt_loop = $cxn-&gt;prepare('SELECT username FROM users WHERE user_id = ?'); $userid = (int)$row['user_id']; $stmt_loop-&gt;bind_param('i', $userid); // Forcing $row['user_id'] to int $stmt_loop-&gt;execute(); $stmt_loop-&gt;bind_result($username); $stmt_loop-&gt;fetch(); $stmt_loop-&gt;close(); //Close the local stmt here } } $stmt-&gt;close(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload