Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming a few things, if they're not correct you'll have to modify this stuff but if I'm right, these operations won't need any <code>JOIN</code>s:</p> <ol> <li><code>$userSession</code> variable contains the user who's logged in's ID.</li> <li><code>$userToView</code> variable contains the user who he's trying to view's ID.</li> <li>Your database structure will only have one entry for the two users who are friends, so we have to check both combinations.</li> </ol> <p>To determine if the user is a friend:</p> <pre><code>SELECT user_a, user_b FROM friends WHERE (user_a = $userSession AND user_b = $userToView) OR (user_a = $userToView AND user_b = $userSession) LIMIT 1; </code></pre> <p>Then you'd use an <code>if</code> statement in PHP (it's usually better to keep business logic in the application, rather than in the database), if it's true return the posts.</p> <pre><code>$results = $mysqli-&gt;query($query)-&gt;fetch_row(); if($results) { ... return posts ... } else { echo 'Not friends'; } </code></pre> <p>That would be a query something like this:</p> <pre><code>SELECT * FROM posts WHERE user = $userToView; </code></pre> <p>You will need to read up about <a href="http://php.net/manual/en/book.mysqli.php" rel="nofollow">mysqli</a> (do not use mysql plugin, it's deprecated) and about <a href="http://php.net/manual/en/security.database.sql-injection.php" rel="nofollow">SQL injection attacks</a> and how to prevent them. It may sound like a hassle, but it's far less daunting to understand this stuff than to deal with the fallout if you don't.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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