Fetching money from SQL, adding them together and dividing by all users?

Discussion in 'PHP' started by CuBz, Aug 12, 2010.

  1. #1
    What code would I use to add together all amounts from 'money' in the users table and divide them by all users?

    Thanks in advance

    EDIT: Is this right?...

    <?php
    session_start();
    include_once"includes/db_connect.php";
    
    $select = mysql_query("SELECT * FROM users WHERE userlevel != '6' ORDER by id");
    $num = mysql_fetch_object($select);
    $numm = mysql_num_rows($select);
    
    
    $amount=ROUND($num->money/$numm, 0);
    
    
    
    echo"$num->money divided by $numm = $amount";
    ?>
    PHP:
     
    Last edited: Aug 12, 2010
    CuBz, Aug 12, 2010 IP
  2. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why not let mysql do all the work? This select statement will return one row with one column containing the value you want:

    
    select ROUND(SUM(money)/COUNT(*)) FROM users WHERE userlevel != '6'
    
    PHP:
    (substitute the correct money field name for "money")
     
    sea otter, Aug 12, 2010 IP