me102 Posted June 1, 2008 Share Posted June 1, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/ Share on other sites More sharing options...
AndyB Posted June 1, 2008 Share Posted June 1, 2008 http://www.tizag.com/mysqlTutorial/mysqlsum.php Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-555219 Share on other sites More sharing options...
me102 Posted June 1, 2008 Author Share Posted June 1, 2008 The problem is the numbers have + and - before the numbers will this still work? Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-555221 Share on other sites More sharing options...
PFMaBiSmAd Posted June 2, 2008 Share Posted June 2, 2008 There is one sure way of finding out, try it and see if it works. Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-555295 Share on other sites More sharing options...
me102 Posted June 2, 2008 Author Share Posted June 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-555337 Share on other sites More sharing options...
sasa Posted June 2, 2008 Share Posted June 2, 2008 for your data SUM(`field_name`) return -45. is it OK? Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-555375 Share on other sites More sharing options...
Barand Posted June 2, 2008 Share Posted June 2, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-555387 Share on other sites More sharing options...
Clinton Posted June 7, 2008 Share Posted June 7, 2008 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 />"; Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-559556 Share on other sites More sharing options...
Barand Posted June 7, 2008 Share Posted June 7, 2008 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 />"; Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-559560 Share on other sites More sharing options...
me102 Posted June 8, 2008 Author Share Posted June 8, 2008 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-560259 Share on other sites More sharing options...
corbin Posted June 8, 2008 Share Posted June 8, 2008 What datatype is amount? Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-560274 Share on other sites More sharing options...
me102 Posted June 8, 2008 Author Share Posted June 8, 2008 What do you mean? Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-560279 Share on other sites More sharing options...
corbin Posted June 8, 2008 Share Posted June 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-560292 Share on other sites More sharing options...
Barand Posted June 8, 2008 Share Posted June 8, 2008 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" Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-560316 Share on other sites More sharing options...
me102 Posted June 8, 2008 Author Share Posted June 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-560385 Share on other sites More sharing options...
corbin Posted June 8, 2008 Share Posted June 8, 2008 Oh wow.... I'm blind; didn't see that lol. Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-560756 Share on other sites More sharing options...
Barand Posted June 8, 2008 Share Posted June 8, 2008 Oh wow.... I'm blind; ... Agreed Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-560768 Share on other sites More sharing options...
corbin Posted June 9, 2008 Share Posted June 9, 2008 <offtopic (sorry)> That's hateful. Nice place to cut it off though. </offtopic> Quote Link to comment https://forums.phpfreaks.com/topic/108297-adding/#findComment-560892 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.