title: How To Use Key Value in Stored Array in New Select Query
body: <p>I'm working in PHP and MySQL to create and list a membership directory.  I have three tables - company, contact and branch. Companies have Contact people, and some but not all companies have Branches, which also have Contact people.  I am using LEFT JOIN in my first query to connect the contact people to their respective company, and loading the results into an array, which works using the following code:</p>
  
  <pre><code>// Retrieve all the data from the "company" table
  $query = "SELECT * FROM company LEFT JOIN contact ON company.company_id = contact.company_id WHERE comp_county = 'BERNALILLO' ORDER BY company.comp_name, contact.cont_rank"; 
  
  $result = mysql_query($query)
  or die(mysql_error());
  
  // Build the $company_array
  $company_array = array();
  while($row = mysql_fetch_assoc($result)) {
      $company_array[] = $row;
  }
  </code></pre>
  
  <p>Now I am trying to figure out how to run a second query, which needs to select from my branch table all branches whose company_id match the company_id stored in my above array: $company_array.  I then want to store the results of the second query into a second array called $branch_array.  I have tried this:</p>
  
  <pre><code>// Retrieve all the matching data from the "branch" table
  foreach($company_array as $row) {
  $query2 = "SELECT * FROM branch LEFT JOIN contact ON branch.branch_id = contact.branch_id WHERE branch.company_id = '".$row['company_id']."' ORDER BY branch.br_name, contact.cont_rank";
  $result2 = mysql_query($query2)
  or die(mysql_error());
  }
  // Build the $branch_array
  $branch_array = array();
  while($row2 = mysql_fetch_assoc($result2)) {
      $branch_array[] = $row2;
  }
  </code></pre>
  
  <p>But this does not seem to work... Can anyone give me an example of how to do this?  The query needs to run so that it checks each different company_id in my $company_array for a match in the branch table - hopefully the question makes sense.  Thanks.</p>