Forum Moderators: coopster

Message Too Old, No Replies

PHP or MySQL Calculations

         

camilord

12:48 am on Jul 23, 2007 (gmt 0)

10+ Year Member



rarely but it happens, php miscalculate... which is better, PHP Calculation or MySQL calculation...

sample for php...

=======PHP=APPROACH=======================================

$CurrentMoney = mysql_result($result, 0, "Money"); // value: 70943247
$intrest = 403924;

$newMoney = $money + $intrest;

mysql_query(sprintf("UPDATE jos_users SET Money = %d WHERE uid = %d", mysql_real_escape_string($newMoney), mysql_real_escape_string($_SESSION["UID"])));

======================================================

=======MYSQL=APPROACH=====================================

$intrest = 403924;

mysql_query(sprintf("UPDATE jos_users SET Money = (Money + %d) WHERE uid = %d", mysql_real_escape_string($interest), mysql_real_escape_string($_SESSION["UID"])));

========================================================

which is better and accurate? i need exact calculation or avoiding miscalculations....

eelixduppy

7:30 am on Jul 23, 2007 (gmt 0)



If I were you I'd use the MySQL method because it is neat and probably the fastest, but for that you can test that yourself with microtime [php.net](). Still, why would you run two queries using the PHP method when you only need to run one.

camilord

4:56 pm on Jul 27, 2007 (gmt 0)

10+ Year Member



thanks...

so far in your experience, does mysql have miscalculation?

distorto

5:27 pm on Jul 27, 2007 (gmt 0)

10+ Year Member



I don't think php itself is capable of miscalculations. It's just a programming language ie: gigo. A poorly written application might miscalculate something. I guess you could consider roundoff errors miscalculations, but developers are supposed to know about things like that. Hmm. Am I wrong? Does php make "miscalculations"?

whoisgregg

5:34 pm on Jul 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All computers experience issues with calculating float numbers (numbers with numbers on the right side of the decimal place).

Check out the manual page for details on MySQL's float handling [dev.mysql.com].

camilord

5:45 pm on Jul 27, 2007 (gmt 0)

10+ Year Member



thanks web masters of masters... :bow: