First off, please don't use the mysql*
functions as they are deprecated. Move to the mysqli*
functions. See the PHP Docs for more details.
How about this:
SELECT CONCAT('$', ROUND(SUM(`purchase`), 2)) AS `result`
FROM `dtable`
CONCAT
just concatenates fields together so, in your case, you would end up with $###2
. You need a function (like ROUND
) to format the number as you want. ROUND
take the number of desired decimal places as the first argument. More on ROUND
(and other MySQL math functions) here: https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_round
You could also do this all in PHP using number_format.
Change
$query = "SELECT purchase concat('$', sum(purchase), 2) from dtable";
....
echo "Total ". $row['SUM(purchase)'];
to
$query = "SELECT SUM(`purchase`) AS `sumOfPurchase` FROM `dtable`";
....
echo 'Total $' . number_format($row['sumOfPurchase'], 2);