Obtain SUM from table column

Discussion in 'PHP' started by MakeThatDollar, Mar 10, 2007.

  1. #1
    Ok I have a column called earn in my users table. How can I get the sum of all the earn totals from the whole table?
     
    MakeThatDollar, Mar 10, 2007 IP
  2. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you mean SQL to get the total? If so here it is

    select sum(earn) from users.

    if not disgrard or tell in more
     
    kashem, Mar 10, 2007 IP
  3. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #3
    kashem is right.

    MakeThatDollar, you can also write in this way:

    "select sum(earn) as total from users";

    then print the total through a variable.
     
    srobona, Mar 10, 2007 IP
  4. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #4
    Here's what I have so far:

    Query to get the total:
    $sumquery = "SELECT sum(earn) FROM users";
    $sumearn = mysql_query($sumquery);

    Code to display the total:
    <td align="center"><b><? echo $sumearn; ?></b></td>

    What is actually displayed:
    Resource id #48

    I'm not sure why it's display "Resource id #48" when it should be display a total like $x.xx or something.
     
    MakeThatDollar, Mar 10, 2007 IP
  5. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #5
    Write like this:

    $sumquery = "SELECT sum(earn) as total FROM users";
    $sumearn = mysql_query($sumquery);
    $rwSum = mysql_fetch_array($sumearn);
    $totalEarn = $rwSum['total'];

    Code to display the total:
    <td align="center"><b><? echo $totalEarn; ?></b></td>
     
    srobona, Mar 10, 2007 IP
    MakeThatDollar and rightit like this.
  6. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #6
    Woohoo! That worked. Rep added. Thanks! :D
     
    MakeThatDollar, Mar 10, 2007 IP
  7. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #7

    You are most welcome :p
     
    srobona, Mar 10, 2007 IP