Adding/Subtracting Money in php?

$total = mysql_result($query, 0);

Thanks guys, that took care of it.

$result = mysql_query(“SELECT * FROM table”)
or die(mysql_error());
$row = mysql_fetch_array( $result );

I am trying to create a table where it shows Total Bill and Total Payment and Balance.
I want to find difference from first two columns (subtract) and display in the 3rd column. how can i do that.
please let me know, i am new to php and mysql.
thanks.

This is a slightly oldish thread, but since nobody mentioned it, I’m going to anyway. Never, ever use floating point values to represent money. Really.
In PHP that means that you shouldn’t do the following:


number_format(4.2 + 2.2, 2)

It won’t give you the result, you expect. Instead, use cents (or what ever is the smallest unit in the relevant currency) as the main unit and store it as an integer. Do all calculations in this unit, keeping the type as integer, and only convert (Eg. divide by 100) when presenting the value.

The advice to use decimal as column value in MySql is fine, since decimal isn’t a floating point value. Just be ware, that it may be implicitly cast to a floating point in PHP. Therefore, multiply it by 100 in the SQL-query, so you’re sure to get an integer out.

i stored in MySql the dollar amount as DECIMAL. in PHP it prints as the way i stored (eg. 1200.00).
but my problem is that, how to do subtraction from one column from another one.
and display or store to the 3rd column as i mentioned in my first post.
Thanks for the quick response.
mhb

I see you’re new here, responses are likely within 10 mins on a good day :slight_smile:

Make a new thread, you’re much more likely to get an answer. I know what you’re after and what you need to do, but could you explain further (e.g. current query, fields etc)