How to calculate rows in query PHP MySQL?


Problem :

I have to 2 rows of query of sum_hour(double type) in database

1st row = sum_hour = 2.75

2nd row = sum_hour = 3.00

It belongs inside 2(two) groups which is type_move = 'P' and (start date >= 2016-11-11 AND end_date <= 2016-11-15) group. The code as below :

<?php
    $sql    = "SELECT  sum_hour,
                    SUM(sum_hour)
                    FROM table
                    WHERE type_move = 'P' AND user_id='username' AND (start_date >= '2016-11-11' AND end_date <= '2016-11-15')
                    GROUP BY sum_hour ";

    $result = mysqli_query($connect, $sql);
    if($result && mysqli_num_rows($result) > 0)
    {
        while($row = mysqli_fetch_assoc($result))
        {   
            $hour = $row['sum_hour'];
            echo " Hours = " .$hour. "<br/>";
            print_r($row);
        }

        echo "Total hours = " .$hour. "<br/>";

    mysqli_free_result($result);
    }
    else
    {
        echo mysqli_error($connect);
    }
?>

And the output as below :

Hours = 2.75

Hours = 3.00

Total hours = ?

My question is , how to calculate 2 rows (Hours) above ? I tried to run sql statement in phpmyadmin. It work and i can see the value sum up into = 5.75 but i don't know how to echo the total in browser

Thanks.



Solution :

Just update your loop with this:

$totalHours = 0;

while($row = mysqli_fetch_assoc($result))
    {   
        $hour = $row['sum_hour'];

        $totalHours += $hour;

        echo " Hours = " .$hour. "<br/>";
        print_r($row);
    }

echo "Total hours = " .$totalHours. "<br/>";

You need to store the first value in a variable then add the second one to the first value. Just make sure that the value can be read outside the loop.


Mysql Tutorials

Mysql Howto..

How to add time to datetime field for all rows in mysql table when field is part of a unique key?

How to split string with date as delimiter?

How to get mysql to accept sybase database..table notation

How to store record changes in a mysql table

How do i search from serialize field in mysql database?

How to use concated variable into mysql query inside mysql stored procedure?

How to use SELECT inside COUNT in MYSQL

How to search for multiple values in a single field of mysql table using codeigniter active record?

How to make a 1 column query by choosing from two columns mysql

How to write this MySQL Query?

How do I know the offset of a time zone from GMT by using its id in joda?

How to draw polygon in HERE Map from polygon data stored in mysql

In MySQL, how do I do an insert based on a join condition?

How to query data for one week per time in mySQL

How to make multiple group counts in MySQL

How to upload photo with text?

How can I cast an int to a bit in MySQL 5.1?

MySQL: how to set a different config file on Windows?

How do I create time to be store as mysql TIMESTAMP type, in PHP?

how to make customised auto increment primary key in mysql

mysql, how to join this key id?

how to insert data from scrapy to mysql

MySql: How to update a single row occurrence from a table that has rows with duplicate fields?

How to properly initialize database connection in Phalcon

How to find Total of sum of rows in mysql?

Rails 3 - how to find last inserted ID from mysql table

How does MySQL use ORDER BY when referencing an indexed prefix on VARCHAR?

How to match password username with php mysql with login form

How to prevent MySQL user-defined variables collision

How to return all matches in mysql where value occurs in string at position n?