title: Simple sql multi-table query
body: <p>I am programming a chat room on my site but I am really new to php. I want users to be able to chat with the users that played in the same teams of a game (knowing that users can have participated together to differents team) and who work in the same area.</p>
  
  <p>Assume there are three tables : the account user's table, the area's t  able, games'table</p>
  
  <p>I have a function that returns my query that looks like </p>
  
  <pre><code>function myfunction($userid){
  
  $games_user=mysql_query('select theme from games where games.userid="'.$userid.'"');  
  $games_theme = mysql_fetch_array($games_user);
  
  $sql = ("select  userid, username, area.userid 
  
      from account 
          left join area
              on account.userid = area.userid    
  
          left join games
              on account.userid = games.userid
  
      where account.userid &lt;&gt; '".mysql_real_escape_string($userid)."' and '".(in_array(games.theme,$games_theme))."' and area.userid=1 
   );
   return $sql;
  }
  </code></pre>
  
  <p><em>Reformatted:</em></p>
  
  <pre><code>$sql = "
  SELECT userid, username, area.userid 
  FROM account 
  LEFT JOIN area ON account.userid = area.userid    
  LEFT JOIN games ON account.userid = games.userid
  WHERE account.userid &lt;&gt; '".mysql_real_escape_string($userid)."'
    AND '".(in_array(games.theme,$games_theme))."'
    AND area.userid = 1 
  ";
  </code></pre>
  
  <p>But it really doesn't work, I think I have syntax problems. 
   I don't really understand how in_array is indexed, and I don't know how to do in a simpler way that query</p>
  
  <p>Can anybody help ?</p>