Jump to content

Adding multiple column values


thereaper87

Recommended Posts

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

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.