PHP While loop displaying only last row
<p>I'm using <strong>TCPDF</strong> library to generate PDF of a table .</p>

<p>Table has few number of rows say 10 </p>

<p>Among that two rows has same invoice number <strong>"78650"</strong></p>

<p>Now i am <strong>selecting by invoice number</strong> and as desired it should generate PDF with <strong>two rows</strong>.</p>

<p>But instead it is only fetching the <strong>second row</strong> that is the last row. That is Serial no 2 is only taken .</p>

<p><strong><em>Code Below :</em></strong></p>

<pre><code>&lt;?php
require_once('TCPDF/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf-&gt;SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf-&gt;SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf-&gt;setLanguageArray($l);
}
$pdf-&gt;SetFont('helvetica', '', 9);
$pdf-&gt;AddPage();

$html1 = '&lt;table  cellspacing="0" class="table table-striped"&gt;
&lt;tr&gt;

&lt;th&gt;SL No.&lt;/th&gt;

&lt;th&gt;Product&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Qty&lt;/th&gt;
&lt;th&gt;Total&lt;/th&gt;
&lt;/tr&gt;';

$sql = ("select * from invoice WHERE invoice_no = 78650 ORDER BY invoice_id ASC");
$result=mysqli_query($connection,$sql);
$i = 1;
while($row = mysqli_fetch_array($result)){
$pr = $row['product'];  
$dr = $row['description'];  
$qty = $row['qty']; 
$total = $row['total']; 

$html2 =  "&lt;tr&gt;

&lt;td&gt;".$i."&lt;/td&gt;                         
&lt;td&gt;".$pr."&lt;/td&gt;
&lt;td&gt;".$dr."&lt;/td&gt;
&lt;td&gt;".$qty."&lt;/td&gt;
&lt;td&gt;".$total."&lt;/td&gt;
&lt;/tr&gt;";

$i++; } 

$sql_1 = ("select *,SUM(total)as tot from invoice WHERE invoice_no = 78650 GROUP BY invoice_no");
$result_1=mysqli_query($connection,$sql_1);
$row_1 = mysqli_fetch_array($result_1);
$tot = $row_1['tot']; 
$html3 =    "&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;

&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Total: &lt;/td&gt;
&lt;td&gt;".$tot."&lt;/td&gt;
&lt;/tr&gt;                       
&lt;/table&gt;";
$html = $html1.$html2.$html3;
$pdf-&gt;writeHTML($html, true, 0, true, 0);
$pdf-&gt;lastPage();
$pdf-&gt;Output('htmlout.pdf', 'I');
?&gt;
</code></pre>