Adding variables

Discussion in 'PHP' started by emi87, Jun 30, 2009.

  1. #1
    Hello,
    I have a mysql table with one column (pieces).
    In the column pieces are many values e.g.(10 15 2).
    How to select from table and add all the values

    $query = "SELECT * FROM stats";
    
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    PHP:
    and now I want something like
    $total = $row['pieces']+$row['pieces']+...etc to be 27 in my e.g.;

    to add all pieces and put the value in a variable
    thank you
     
    emi87, Jun 30, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Run a quick query like this:

    $query = "SELECT SUM(pieces) as total FROM stats";

    $result = mysql_query($query);
    $total = mysql_fetch_array($result);

    echo $total['total'];
     
    jestep, Jun 30, 2009 IP
  3. emi87

    emi87 Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    IS WORKING! :D
    Thank you very much jestep
    >:D<
     
    emi87, Jun 30, 2009 IP