I was trying to calculate the total of each column in the database and display it on the webpage using php. But it just gives me
Array
(
[0] => 6
[sum(food1)] => 6
)
which I just want '6' as my result.
Here is my code
for($i=1; $i<=10; $i++){
$foodid=('food'."$i");
echo $foodid;
$food_query = mysql_query("select sum($foodid) from orderdetail where date between '$fm_date' and '$to_date'");
$ttl_food= mysql_fetch_array($food_query);
print_r($ttl_food[$i]);
}
Thanks so much!
mysql_fetch_row. Note: Please, don't usemysql_*functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.