thereaper87 Posted September 26, 2010 Share Posted September 26, 2010 Hello, I am trying to add all rows in the same column together. So here is the basic scheme: id l column1 1 l 1 2 l 2 3 l 3 Second table id l variable1 1 l 3 2 l 5 3 l 6 I basically want to add 5+3+6 and set it to a variable named '$total'. Would this be done using while loops? Here is the code I have been messing with. $sql="SELECT column1 from table1 where id='$userid'"; // userid would be set near the top of the page $sql2=mysql_query($sql,$conn); while($rows=mysql_fetch_array($sql2)){ $getid = $rows['column1']; $sql3="SELECT variable1 FROM table2 where id='$getid'"; $sql4=mysql_query($sql3,$conn); while($items=mysql_fetch_array($sql4)){ // Then I'm getting lost... } } Can you at least see what I am trying to do? I'm not even sure if this is the right approach, any help would be appreciated! Link to comment https://forums.phpfreaks.com/topic/214480-adding-multiple-column-values/ Share on other sites More sharing options...
litebearer Posted September 26, 2010 Share Posted September 26, 2010 Perhaps this will get you started.. http://www.tizag.com/mysqlTutorial/mysqlsum.php Link to comment https://forums.phpfreaks.com/topic/214480-adding-multiple-column-values/#findComment-1116085 Share on other sites More sharing options...
thereaper87 Posted September 26, 2010 Author Share Posted September 26, 2010 thank you for that link and I have bookmarked it! It may end up being useful, but how could I reword this 'GROUP BY type' to fit my query? There really isn't another column that would work the same way. Link to comment https://forums.phpfreaks.com/topic/214480-adding-multiple-column-values/#findComment-1116094 Share on other sites More sharing options...
litebearer Posted September 27, 2010 Share Posted September 27, 2010 untested... $query="SELECT sum(variable1) as total_variable FROM table2"; $result = mysql_query($query) $my_row = mysql_fetch_array($result); $total = $my_row['total_variable']; echo "the total is: $total; Link to comment https://forums.phpfreaks.com/topic/214480-adding-multiple-column-values/#findComment-1116101 Share on other sites More sharing options...
thereaper87 Posted September 27, 2010 Author Share Posted September 27, 2010 Awesome! I will try to fit it into my code and get it working, thanks a mil! Link to comment https://forums.phpfreaks.com/topic/214480-adding-multiple-column-values/#findComment-1116102 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.