title: Combining several SQL queries with while statements into 1 query
body: <p>OK I am trying to clean up my mess of a script. I am still relatively new to SQL queries. So here is my queries as of now.</p>
  
  <pre><code>$query = "SELECT id FROM ow_photo_album WHERE userId = '$id' ORDER by ID";
  $result = $con1-&gt;query($query);
  
  while($row = $result-&gt;fetch_row()) {
    $rows[]=$row;
    $aid = $row[0];
  
    $queryb = "SELECT id FROM ow_protectedphotos_passwords WHERE albumId = '$aid' AND privacy = 'password' ORDER by ID LIMIT 1";
    $checkmoderated = $con1-&gt;query($queryb);
  
    $row3 = $checkmoderated-&gt;fetch_row();
    $checked = $row3[0];
  
    if($checked &gt; 0) { 
          $queryP = "SELECT id, hash FROM ow_photo WHERE albumId = '$aid' ORDER by ID";
          $resultP = $con1-&gt;query($queryP);
  
          while($rowP = $resultP-&gt;fetch_row()) {
            $rowsP[]=$rowP;
            $pid = $rowP[0];
            $hash = $rowP[1];
  </code></pre>
  
  <p>So this works for what I needed it to do. it takes the first query and get the users album IDs, then checks with the 2nd query with those ID's to see if the albums are private. Last it then takes the albums that are private ( album ids ) and find all the photos for those albums. </p>
  
  <p>Then I need to out put the photo ID and the Hash column.  </p>
  
  <p>Here is what I have so far. </p>
  
  <pre><code>    $query = "SELECT t.*, tt.*, tr.*
      FROM ow_photo_album AS t 
          INNER JOIN ow_protectedphotos_passwords AS tt
              ON t.id = tt.albumId
          INNER JOIN ow_photo AS tr
              ON tr.albumId = tt.albumId
      WHERE t.userId = '$id'
      ORDER BY tr.id ASC";
  </code></pre>
  
  <p>but the thing is when running something joining several tables together how do i find the correct rows? I am not sure if I could use fetch assoc, because there are 3 tables with 3 columns named 'id'</p>