I have a column name quantity, unit_price and total_amount. The total_amount field in my database set to zero, in my query I used SUM(unit_price*quantity) AS total_amount to get the total value as total_amount. But when I tried to get the sum of total amount as total_sum is always get 0. if is it because the total_amount field in my database is set to zero? What I want to do is get the SUM(total_amount) AS total_sum but it always displays 0.
This is my Query.
<?php
$mysqli = new mysqli("localhost", "root", "", "app");
$term = $_GET['supp'];
$result = $mysqli->query("SELECT *, SUM(unit_cost*quantity) AS total_amount FROM procurement WHERE supplier LIKE '%".$term."%' GROUP BY counter");
echo'<table id="tfhover" cellspacing="0" class="tablesorter" style="text-transform:uppercase;" border="1px">
<thead>
<tr>
<th>SUPPLIER</th>
<th>ITEM</th>
<th>DESCRIPTION</th>
<th>QTY</th>
<th>UNIT</th>
<th>UNIT PRICE</th>
<th>Total Amount</th>
</tr>
</thead>';
echo'<tbody>';
while($row = $result->fetch_assoc()){
echo'<tr>
<td>'.$row['supplier'].'</td>
<td>'.$row['item_name'].'</td>
<td>'.$row['item_description'].'</td>
<td>'.$row['quantity'].'</td>
<td>'.$row['unit'].'</td>
<td>'.number_format($row['unit_cost'], 2, '.', ',').'</td>
<td>'.number_format($row['total_amount'], 2, '.', ',').'</td>
</tr>';
}
echo'<TR> <TD COLSPAN=6 BGCOLOR="#99CCFF">TOTAL AMOUNT</TD> <td>'.number_format($row['total_sum'], 2, '.', ',').'</td></TR>';
echo "</tbody></table>";
?>
My another problem is when I insert this code echo' TOTAL AMOUNT '.number_format($row['total_sum'], 2, '.', ',').''; inside the while loop. I want to have just 1 total amount row.
SELECT *, SUM(unit_cost*quantity) AS total_amount FROM procurement WHERE supplier LIKE '%".$term."%' GROUP BY counter"
This is a select statement which would not update any data in the database. It seems that your total_amount
, which can be evaluated every time when you need it, is not necessarily exists as a column in your table.
Also, can you post your table structure ? Actually the SQL seems not correct anyway.