cwncool Posted February 25, 2007 Share Posted February 25, 2007 I'm having a problem with a simple addition script. If I echo the variables, 'old' and 'new' that will be used to add together, they echo fine, as numbers, but when I try to echo the total, 'total', it results in "1E-05", which obviously isn't a correct sum. Can someone tell me what's wrong with this script? <?php include 'connect.php'; if(isset($_GET['id'])) { $id = $_GET['id']; $mdata = mysql_query("SELECT * FROM users WHERE id='$id'"); $mdata = mysql_fetch_array($mdata); $old = $mdata['earn']; $new = '0.00001'; $total = $old+$new; echo $id; echo '<br><br>'; echo $old; echo '<br><br>'; echo $new; echo '<br><br>'; echo $total; } ?> Link to comment https://forums.phpfreaks.com/topic/39995-php-addition-problem-should-be-easy/ Share on other sites More sharing options...
Archadian Posted February 25, 2007 Share Posted February 25, 2007 the number_format() function might help. http://us3.php.net/manual/en/function.number-format.php Link to comment https://forums.phpfreaks.com/topic/39995-php-addition-problem-should-be-easy/#findComment-193415 Share on other sites More sharing options...
Hooker Posted February 25, 2007 Share Posted February 25, 2007 Try this: <?php include 'connect.php'; if(isset($_GET['id'])) { $id = $_GET['id']; $new = '0.00001'; $mdata = mysql_query("SELECT * FROM users WHERE id='$id'"); while ($row = mysql_fetch_array($mdata, MYSQL_ASSOC)) { $old = $row['earn']; } $total = $old + $new; echo $id; echo '<br><br>'; echo $old; echo '<br><br>'; echo $new; echo '<br><br>'; echo $total; } mysql_free_result($mdata); ?> Not completely tested (i.e - i didn't create a db, i manualy inputted the value of $old) but it should work! Link to comment https://forums.phpfreaks.com/topic/39995-php-addition-problem-should-be-easy/#findComment-193422 Share on other sites More sharing options...
AndyB Posted February 25, 2007 Share Posted February 25, 2007 Best guess, earn is not a number, it's a string variable (e.g. text/varchar). If so, change $new = '0.00001' to $new = 0.00001 Link to comment https://forums.phpfreaks.com/topic/39995-php-addition-problem-should-be-easy/#findComment-193559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.