www.webdeveloper.com
Results 1 to 2 of 2

Thread: Adding data (sum) from a column

  1. #1
    Join Date
    Dec 2006
    Posts
    68

    resolved Adding data (sum) from a column

    How do I sum data from a certain colum in a mysql database.

    like if my data is

    choice | total
    ---------------
    opt a | 5
    opt b | 3
    opt c | 7


    How would I add up those totals? I ended up trying this but it didn't work.

    PHP Code:
    $query mysql_query("SELECT * FROM test")
    while (
    $row mysql_fetch_array($query)) {
    $a = array($row['total']);
    echo 
    array_sum($a);

    (P.S. I'd like to do this without omitting the "choice" column in the table select)
    Last edited by blade52x; 01-17-2007 at 01:27 PM.

  2. #2
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    PHP Code:
    <?php
    $query 
    mysql_query("SELECT * FROM test")
    $total 0;
    while (
    $row mysql_fetch_array($query)) {
      echo 
    $row['choice'] . ": " $row['total'] . "<br>";
      
    $total += $row['total'];

    echo 
    "Total: " $total;
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles