 <p>Using joins would make it easier to follow, and afraid you have not said which field is giving a duplicate.</p> <p>For example the following is a quick hack (untested) at you code to eliminate some of the separate selects:-</p> <pre><code>&lt;?php $SQL = "SELECT ID, ItemID FROM IR_Logs"; $data = $db-&gt;getResults($SQL); $count = mysql_num_rows($data); echo '&lt;p&gt;Returned Results: '.$count.'&lt;/p&gt;'; while($row = mysql_fetch_assoc($data)) { $ItemID = $row['ItemID']; $SQL = "SELECT cs.SiteManager, cs.CompanyID, cs.Reference, cu.Name, cu.Telephone, cu.Email FROM IR_Logs ir INNER JOIN Customer_Sites cs ON ir.LocationID = cs.ID INNER JOIN Customer_Users cu ON cs.CompanyID = cu.CompanyID AND cu.ID = cs.SiteManager"; $LocationData = $db-&gt;getResults($SQL); while($row2= mysql_fetch_assoc($LocationData)) { $SiteManagerID = $row2['SiteManager']; $CompanyID = $row2['CompanyID']; $SiteReference = $row2['Reference']; $Manager_Name = $row3['Name']; $Manager_Telephone = $row3['Telephone']; $Manager_Email = $row3['Email']; $SQL = "SELECT i.Reference, ca.Company, cs.Reference, a.Type, DATE_FORMAT(a.Date, '%d %M %Y'), a.CompanyID, a.LocationID FROM IR_Logs a LEFT OUTER JOIN Inventory i ON a.ItemID = i.ID LEFT OUTER JOIN Customer_Accounts ca ON a.CompanyID = ca.ID LEFT OUTER JOIN Customer_Sites cs ON a.LocationID = cs.ID WHERE a.LocationID = $LocationID ORDER BY CompanyID ASC"; $LogData = $db-&gt;getResults($SQL); if(mysql_num_rows($LogData) &lt;&gt; 0){ $HQEmail = $db-&gt;getValue("SELECT PrimaryEmail FROM Customer_Accounts WHERE ID = $CompanyID"); $message .= '&lt;h3&gt;Site Ref: '.$SiteReference.'&lt;/h3&gt;'; $message .= '&lt;p&gt;For the Attention of '.$Manager_Name.', in regards to Site Reference: '.$SiteReference.'&lt;/p&gt;'; $message .= $blk-&gt;GetBlock(6); $message .= '&lt;table style="border:1px solid black; padding: 10px;" cellpadding="10"&gt;'; $message .= '&lt;tr&gt;'; $message .= '&lt;td&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;/td&gt;'; $message .= '&lt;td&gt;&lt;strong&gt;Type&lt;/strong&gt;&lt;/td&gt;'; $message .= '&lt;td&gt;&lt;strong&gt;Date&lt;/strong&gt;&lt;/td&gt;'; $message .= '&lt;/tr&gt;'; while($row4 = mysql_fetch_array($LogData)){ $Reference = $row4[0]; $Location = $row4[2]; $Company = $row4[1]; $Type = $row4[3]; $Date = $row4[4]; $CompanyID = $row4[5]; $LocationID= $row4[6]; $message .= '&lt;tr&gt;'; $message .= '&lt;td&gt;'.$Reference.'&lt;/td&gt;'; $message .= '&lt;td&gt;'.$Type.'&lt;/td&gt;'; $message .= '&lt;td&gt;'.$Date.'&lt;/td&gt;'; $message .= '&lt;/tr&gt;'; } $message .= '&lt;/table&gt;'; } } } ?&gt; </code></pre>