Outputting summary of totals in PHP

Hi, I need to find a way of outputting the total (sum) values of three decimal fields in a mySQL table using PHP.

I thought this might be a good query string to use:


$query = "SELECT Nett, SUM(Nett), VAT, SUM(VAT), Gross, SUM(Gross) from Customers WHERE Name LIKE '$name'";

This will act on data from a previous page where the user selects the Customer from a dropdown- and this produces the $name variable.

Nett, VAT and Gross are the three fields. There are about 50 or more values for each, for example Nett values might be -123.54, -234.76 and so on. For each of these three fields, I want to be able to add them all up (for that specified Customer name) so we end up with the added-up values of all three fields in the table that fit the criteria (i.e which are for that Customer).

Assuming the query is ok (and it probably isn’t), I need to find a way of getting PHP to do the work, and output a variable which equals the respective totals. What would be the best way of doing this?

I’ve read this three times now and am still unsure of what is being asked.

Are you asking for help with the sql in order to do the math or help to do the math in PHP or are you asking about general pointers about which layer of your application should be responsible for working out the final sums, or pointers about the best variable types to handle math?

How about showing the outcome of a row of realistic test data?

Thanks for the reply. Have been pointed to a fix / solution for this now.