It's not obvious at all, because no-one here knows what $moneyearned and $totaltokens are, so their ratio could be anything, including 1.397.
What can be said is that you don't want to go formatting $totaltokens as a string with rounding, commas and so on; you want to leave it as a number.
I don't know if MySQL is up to it, but you might be able to do everything (the select, the division, and the update) with a single query. I'm pretty sure you could write "SELECT sum(balances)+sum(refearnings) as totaltokens" at least.