Lamez Posted December 19, 2008 Share Posted December 19, 2008 I cannot seem to wrap my head around this. In my database I have a rows filled with information, and one of the columns is pnt, and I want to take the pnt columns for that user and add them together for a total. I have created a function, here it is so far: <?php function findPnt($usr){ //adds up the points to find the total points for a user in the football pool $q = mysql_query("SELECT * FROM `foot_picks` WHERE `user` = '".$usr."'"); while($p = mysql_fetch_array($q)or die(mysql_error())){ $tot = $p['pnt'] + $p['pnt']; } return $tot; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137747-solved-simple-addition-and-mysql/ Share on other sites More sharing options...
.josh Posted December 19, 2008 Share Posted December 19, 2008 $q = "select sum(column) as total from table where user= '$user'"; $result = mysql_query($q); $r = mysql_fetch_assoc($result); echo $r['total']; Quote Link to comment https://forums.phpfreaks.com/topic/137747-solved-simple-addition-and-mysql/#findComment-719991 Share on other sites More sharing options...
Lamez Posted December 19, 2008 Author Share Posted December 19, 2008 if the sum comes out to be 0, will it not display anything? Quote Link to comment https://forums.phpfreaks.com/topic/137747-solved-simple-addition-and-mysql/#findComment-719998 Share on other sites More sharing options...
.josh Posted December 19, 2008 Share Posted December 19, 2008 well that depends on your table structure. If for instance, default value is null, and every single row being selected is null...null+null+null = null. But if default is like 0...0+0+0 = 0. 0 is a valid number. Quote Link to comment https://forums.phpfreaks.com/topic/137747-solved-simple-addition-and-mysql/#findComment-720005 Share on other sites More sharing options...
Lamez Posted December 19, 2008 Author Share Posted December 19, 2008 well, it returned 0. I know what I did. -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/137747-solved-simple-addition-and-mysql/#findComment-720008 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.