1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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