Jump to content

PHP addition problem - Should be easy...


cwncool

Recommended Posts

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

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.