number of users

Discussion in 'PHP' started by roice, Aug 23, 2012.

  1. #1
    Hello,

    If I want to print the total number of users that register in my website (at the top of the website), do I need to calculate the number in every page that user goes?

    mysql_query("SELECT id FROM `users` ");
    echo mysql_num_rows($query);

    is there any better way to keep this number and print it instand of calculte it every time (every page)?


    Roi.
     
    roice, Aug 23, 2012 IP
  2. JamesColin

    JamesColin Prominent Member

    Messages:
    7,874
    Likes Received:
    164
    Best Answers:
    1
    Trophy Points:
    395
    Digital Goods:
    1
    #2
    Yes, you should store this number in a file on your web server, and just update it every hour or more.
    Like in pseudo code:
    if (file is older than 1 hour) then:
    mysql query and write the result into the file
    output the result in html
    else:
    read the file
    output the result in html
    end if

    I believe reading a file is less ressource hungry than running the mysql query, but maybe things have changed, I don't know.
     
    JamesColin, Aug 23, 2012 IP
  3. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #3
    or you could run a query like this

    
    
    $sql = 'SELECT COUNT(*) AS `total` FROM `users`';
    $query = mysql_query($sql);
    $result = mysql_fetch_assoc($query);
    
    echo 'Total number of users: '.$result['total'];
    
    
    PHP:

    let the database do the counting. it is a lot quicker and less data that is returned that doing it your way.

    Also storing it in a temp file is a rubbish idea. You still end up having to read a file etc and I a database is a lot quicker.
     
    plussy, Aug 23, 2012 IP
  4. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #4
    For small databases it's not much of an issue, me personally I would create a table for totals and every time a user is added/deleted just updated the amount stored in the column.
     
    HuggyEssex, Aug 23, 2012 IP
  5. InstaCoders

    InstaCoders Peon

    Messages:
    53
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Why not just create a function for it and then simply call the function out onto the page?

    I also agree - it's easier to do a simple query to get the rows then using a temp file.
    Now there is something else to consider as well. The number of 'active' versus inactive users
    on your site - if you are in fact keeping that data.

    So if you truly wanted 'active' users you would have to use something like:

    $sql = "SELECT * FROM users WHERE active = '1'";
    $res = mysql_query($sql);
    $num = mysql_num_rows($res);

    That would give you the active users for the site that you could put into a function and call anywhere.
     
    InstaCoders, Aug 25, 2012 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    This is the approach usually used in most forum softwares. A "total's" table only needs to be updated when something is added/removed, a fairly infrequent operation... and then you get a small query on a small table with a small results set whenever you want to show that number, a far, far, far more frequent occurrence.

    Blindly going after the table to sum the totals every time you want to show it is just going to be slow and drag server performance down...

    HOWEVER, it is possible for the 'totals' table to end up out of sync, so toss in a admin function to 'recalculate all totals' just in case.
     
    deathshadow, Aug 25, 2012 IP
  7. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    creating temp file or new small table force me to manage them - update the, all the time.
    calcule the total users force the DB to run all over the table and sum the total rows (happened every time that user click on new page).

    I thought that there is a better solution for that...:/
     
    roice, Aug 26, 2012 IP
  8. ikoolo

    ikoolo Greenhorn

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #8
    By using the SELECT count(*) FROM users, this should output the number of rows in the table USERS.
     
    ikoolo, Aug 26, 2012 IP
  9. fastestsms

    fastestsms Greenhorn

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #9
    I think it will be better if you use
    If you want a "realtime update", you have to run that query again and again per each page reload. If not, you should set it to a session then set a delay to auto refresh it. :)
     
    fastestsms, Aug 26, 2012 IP
  10. afstanislav

    afstanislav Greenhorn

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    16
    #10
    Notice about COUNT:

    Better user COUNT(*) but not COUNT(val).
    If you use COUNT(val) and this field have some NULL values this values will be ignored as i remember.

    COUNT(*) it's ok, and this query will be cached as i remember.
     
    afstanislav, Aug 27, 2012 IP