title: Create dynamic html table from sql query result to use for mailing list with phpmailer
body: <p>following my <a href="https://stackoverflow.com/questions/39190457/send-mass-email-with-phpmailer-where-email-body-is-the-result-of-sql-query-in-a">original question</a>, I try to rephrase it to make it more clear:</p>
  
  <p>I have this <a href="https://i.stack.imgur.com/huhMt.jpg" rel="nofollow noreferrer">two tables</a> that are the result of two queries ;</p>
  
  <p>The tables have the first column in common but  each row in the first table can have more than one correspondence in the second.</p>
  
  <p>I need to iterate through each row in the first table and create a dynamic html table with all the correspondent row(s) from the second.</p>
  
  <p>This is the code I tried but it only gives as result the first row from the second table:</p>
  
  <p><code>$stmt2</code> is for table1 and <code>$stmt</code> is for table2
      
  
  <pre><code>$row2 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC);
  foreach ($row2 as $fornitori) {
      $fornitore = $row2['FOURNI'];
  
      //Intestazione della tabella uguale per tutti
  
      echo "&lt;table&gt;&lt;tr&gt;&lt;th&gt;ODL&lt;/th&gt;&lt;th&gt;INV&lt;/th&gt;&lt;th&gt;APP.&lt;/th&gt;&lt;th&gt;SERIALE&lt;/th&gt;&lt;th&gt;MODELLO&lt;/th&gt;&lt;th&gt;MARCA&lt;/th&gt;&lt;/tr&gt;";
      while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
          if ($fornitore == $row['FOURNI']) {
                  echo "&lt;tr&gt;&lt;td&gt;".$row['NU_INT']."&lt;/td&gt;&lt;td&gt;".$row['NU_IMM']."&lt;/td&gt;&lt;td&gt;".$row['NOM_EQP']."&lt;/td&gt;&lt;td&gt;".$row['N_SERI']."&lt;/td&gt;&lt;td&gt;".$row['TYP_MOD']."&lt;/td&gt;&lt;td&gt;".$row['MARQUE']."&lt;/td&gt;&lt;/tr&gt;";
              }
          }
      }
      echo "&lt;/table&gt;";
  ?&gt;
  </code></pre>
  
  <p>These are the two queries:</p>
  
  <p>1 - <code>$sql</code> that goes to <code>$stmt</code></p>
  
  <pre><code>$sql = "SELECT [NU_INT],[NU_IMM],[N_SERI],[NOM_EQP],[TYP_MOD],[MARQUE],NOM_UF],[NOM_ETAB],[DA_AP],[OBSERV],[OBSERV2],[LIB_STATUT], A.FOURNI, AD_EMAIL    
  FROM [INPROGRESS_WO_VIEW] A    
  LEFT JOIN    
  FOURNIS2 F    
  ON A.FOURNI = F.FOURNI    
  WHERE A.FOURNI &lt;&gt; 'NULL'    
  ORDER BY A.FOURNI ASC";
  </code></pre>
  
  <p>2 - <code>$sql2</code> that goes to <code>stmt2</code>:</p>
  
  <pre><code>$sql2 = "SELECT DISTINCT A.FOURNI
  FROM FOURNIS2 A
  LEFT JOIN 
  [INPROGRESS_WO_VIEW] B
  ON A.FOURNI = B.FOURNI
  WHERE [NU_INT] &lt;&gt; 'NULL'
  ORDER BY A.FOURNI";
  </code></pre>
  
  <p>I need to to this because I need to send an email to every row in the first table with the information of the second with phpmailer</p>
  
  <p>I hope I've been more precise and clear this time :)</p>
  
  <p>Thank you</p>