Jump to content

Adding


me102

Recommended Posts

Hello I have a database which is filled with numbers which have + or - in it.

 

 

looks like this,

 

+10.00

-100.00

+45.00

 

Now I have been working with php for a long time and dont have a clue how I would get the total.

Link to comment
Share on other sites

There is one sure way of finding out, try it and see if it works.  ::)

 

 

There is another way ::)::)::)::)::)::)::) but ill keep it to myself..... anyways I did try it and didnt work which is why I asked in the first place.

Link to comment
Share on other sites

it certainly does

 

mysql> select colb from abc;
+------+
| colb |
+------+
| +10  |
| -100 |
| +45  |
+------+
3 rows in set (0.00 sec)

mysql> select SUM(colb) from abc;
+-----------+
| SUM(colb) |
+-----------+
|       -45 |
+-----------+
1 row in set (0.00 sec)

Link to comment
Share on other sites

I've got this below - It was working when I just had one column. But when I added more columns, some had numbers in the signin and some didn't, it stopped working. Any idea?

 

$sumadd = mysql_query("SELECT SUM(signin) FROM `$table` GROUP BY `datecode`") or die(mysql_error());

while($row = mysql_fetch_array($sumadd)){extract($row);
echo "<center>Total Add for $choice = $signin";
echo "<br />";

Link to comment
Share on other sites

There is no "signin" column in your result set. Use an alias for the SUM(signin)

 

<?php
$sumadd = mysql_query("SELECT SUM(signin) as signin FROM `$table` GROUP BY `datecode`") or die(mysql_error());

while($row = mysql_fetch_array($sumadd)){extract($row);
echo "<center>Total Add for $choice = $signin";
echo "<br />";

Link to comment
Share on other sites

Can someone tell me whats wrong with this?

 

 

<?php

$total = mysql_query("SELECT SUM(amount) as amount, FROM `transaction` WHERE `user_id` = '$user_id'") or die(mysql_error());
while($row = mysql_fetch_array($total)){
extract($row);

echo $amount;

}

?>

Link to comment
Share on other sites

What datatype is the amount column?  Integer?  varchar?

 

If you don't know how to check, google it.

 

I'm guesing it's some kind of string type since you have a + or -...  (The + probably wouldn't be there if it were a numeric column... unless you're displaying it with modifications.)

 

If it is a string type, MySQL might not add the columns correctly.

Link to comment
Share on other sites

Can someone tell me whats wrong with this?

 

 

<?php

$total = mysql_query("SELECT SUM(amount) as amount, FROM `transaction` WHERE `user_id` = '$user_id'") or die(mysql_error());
while($row = mysql_fetch_array($total)){
extract($row);

echo $amount;

}

?>

 

Syntax is wrong. Remove "," from before the "FROM"

Link to comment
Share on other sites

Can someone tell me whats wrong with this?

 

 

<?php

$total = mysql_query("SELECT SUM(amount) as amount, FROM `transaction` WHERE `user_id` = '$user_id'") or die(mysql_error());
while($row = mysql_fetch_array($total)){
extract($row);

echo $amount;

}

?>

 

Syntax is wrong. Remove "," from before the "FROM"

 

Thank you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.