title: How to join three tables and add a "AND" operator?
body: <p>Being a Rookie i am a bit lost as to how to nail this.  I am trying to join three tables.  This is the table structure: </p>
  
  <pre><code>Accounts Table: 
  
  1) id
  2) User_id (Id given to user upon sign up)
  3) Full_name 
  4) Username
  5) Email
  6) Password
  
  Contacts Table: 
  
  1) My_id (This is the user who added friend_id)
  2) Contact_id (this is the contact who was added by my_id)
  3) Status (status of relationship)
  
  Posts Table: 
  
  1) Post_id
  2) User_posting (this is the user who posted it)
  3) Post_name
  4) User_allowed (this is where its specified who can see it)
  </code></pre>
  
  <p>Here is the code structure i have: </p>
  
  <pre><code>&lt;?php
  
  $everybody = "everybody";
  $sid = "user who is logged in.";
  
  $sql = &lt;&lt;&lt;SQL
  SELECT DISTINCT contacts.contact_id, accounts.full_name, posts.post_id, posts.post_name
  FROM contacts, accounts, posts
  WHERE
      (contacts.my_id = '$sid'
        AND contacts.contact_id = accounts.user_id)
      OR (contacts.my_id = '$sid'
        AND contacts.contact_id = accounts.user_id)
      OR (posts.user_posting = '$sid'
        AND contacts.contact_id = accounts.user_id
        AND posts.user_allowed = '$everybody')
      OR (posts.user_id = '$sid'
      AND contacts.user_id = accounts.user_id
      AND posts.user_allowed = '$everybody')
  
  LIMIT $startrow, 20;
  
  
  $query = mysql_query($sql) or die ("Error: ".mysql_error());
  
  $result = mysql_query($sql);
  
  if ($result == "")
  {
  echo "";
  }
  echo "";
  
  
  $rows = mysql_num_rows($result);
  
  if($rows == 0)
  {
  print("");
  
  }
  elseif($rows &gt; 0)
  {
  while($row = mysql_fetch_array($query))
  {
  
  $contactid = htmlspecialchars($row['contact_id']);
  $name = htmlspecialchars($row['full_name']);
  $postid = htmlspecialchars($row['post_id']);
  $postname = htmlspecialchars($row['post_name']);
  
  // If $dob is empty
  if (empty($contactid)) {
  
  $contactid = "You haven't added any friends yet.";
  }
  
  
  print("$contactid &lt;br&gt;$postname by $name &lt;br /&gt;");
  }
  
  }
  </code></pre>
  
  <p>The problem is that the query shows me my posts plus all posts from friends.  </p>
  
  <p>What am i doing wrong?</p>