title: Selecting from 2 tables using session variable
body: <p>I have a website where users can create SubAccounts and assign them to different locations.I'm trying to  create a page where users are able to see their SubAccounts and the locations they are tied to.In order to achieve that, i'm trying to select and display data from 2 tables where the condition for the SELECT is a session variable which corresponds to a unique UserId.</p>
  
  <p>My Code:</p>
  
  <pre><code>$viewuser="SELECT Username,Password,Name,Info FROM SubUsers,Branch WHERE SubUsers.Userid='$_SESSION[UserId]'";
  $runuser=mysqli_query($cxn,$viewuser) or die(mysqli_error($cxn));
  $rows=array();
  while($row=mysqli_fetch_assoc($runuser))
  $rows[]=$row;
  
  echo"&lt;table border='1'&gt;";
  
  foreach($rows as $row)
  {
  echo"&lt;tr&gt;";
  echo"&lt;td&gt;";
  echo"$row[Username]";
  echo"&lt;/td&gt;";
  echo"&lt;td&gt;";
  echo"$row[Password]";
  echo"&lt;/td&gt;";
  echo"&lt;td&gt;";
  echo"$row[Name]";
  echo"&lt;/td&gt;";
  echo"&lt;td&gt;";
  echo"$row[Info]";
  echo"&lt;/td&gt;";
  echo"&lt;/tr&gt;";
  }
  echo"&lt;/table&gt;";
  </code></pre>
  
  <p>While i am able to successfully select and display the query, the results are in the form of a cross join, which is undesired.</p>
  
  <p>The Result:</p>
  
  <pre><code>Username    Pass    Branch   BranchInfo
  Sub1    pass1   Branch1  1st Branch
  Sub2    pass2   Branch1  1st Branch
  Sub1    pass1   Branch2  2nd Branch
  Sub2    pass2   Branch2  2nd Branch
  </code></pre>
  
  <p>Desired Result:</p>
  
  <pre><code>Username    Pass    Branch   BranchInfo
  Sub1    pass1   Branch1  1st Branch
  Sub2    pass2   Branch2  2nd Branch
  </code></pre>
  
  <p><em><strong>Question</em></strong>
  *<strong><em>How do i get my desired result?</em>*</strong> I've tried looking into JOINS, but it seems to me that JOINS are used to join tables where they have a column/filed in common.However in this case, while both my tables do have a column(UserId) in column, the columns in both tables refer to different things.</p>
  
  <p>As such, i am trying to SELECT such that SubUsers.Userid='$_SESSION[UserId]' and <strong>NOT</strong> SubUsers.Userid=Branch.Userid as Branch.Userid refers to the SubUser's id.This thus prevents me from using JOINS to sort/display the data.</p>
  
  <p>PS:I can have multiple branchs per user, and each branch can have a different username.</p>
  
  <p>EDIT: Included Tables</p>
  
  <p>Branch Table</p>
  
  <pre><code>----------------------------------------------------
  |ID    |BusinessID  |UserId  |Name     |Info       |
  ----------------------------------------------------
  |1     |    1       |   2    |Branch1  | 1st Branch|
  ---------------------------------------------------|
  |2     |    1       |   2    |Branch2  | 2nd Branch|
  ----------------------------------------------------
  </code></pre>
  
  <p>SubUsers Table</p>
  
  <pre><code>-------------------------------------------------------
  |ID     |UserId   |BranchId  |Username  |Password     |
  -------------------------------------------------------
  |1      |2        |1         |Sub1      |pass1        |
  -------------------------------------------------------
  |2      |2        |2         |Sub2      |pass2        |
  -------------------------------------------------------
  </code></pre>
  
  <p>Thanks</p>