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