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.

Would like to display number of members on website

Discussion in 'PHP' started by TwistOffMedia, Aug 4, 2011.

  1. #1
    I own a website (in my signature) and I would like to know how to display the number of members we have. The only way I can see for myself is to count the number of records in the users table in phpmyadmin. Is there a way to display that number publicly ?
     
    TwistOffMedia, Aug 4, 2011 IP
  2. freelancewebaz

    freelancewebaz Well-Known Member

    Messages:
    976
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    145
    #2
    Run a count query on the users table and echo that out.

    SELECT count(*) FROM users; 
     
    freelancewebaz, Aug 4, 2011 IP
  3. TwistOffMedia

    TwistOffMedia Well-Known Member

    Messages:
    3,233
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    150
    #3
    Here's what I got:

    <?php
    Total Users: SELECT echo count(*) FROM users;
    ?>

    I used php require on my index file, and made it require counter.php

    error on line 2

    how do i do this? haha im brute at php
     
    TwistOffMedia, Aug 4, 2011 IP
  4. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #4
    check to see if a number of user cookies is present, if not then read the number of user from the db, store it in a user cookie with an expire date of 2-3 minutes. start from the beginning.

    as for the mysql db, when they login then put that they logged in. Put the time of login. each time they refresh or change page update the time. then have a check routine check to see who has been logged in for 5 minutes without moving to another page or something remove the login from the database. this will work until the site gets really big. after that you should hire a professional to help you with a better file cache/mysql system to check.

    You could look for a open source script that handles the user counts and see how they handle it.
     
    exodus, Aug 4, 2011 IP
  5. echipvina

    echipvina Active Member

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    SELECT count(*) AS numbernember FROM users WHERE group = member
     
    echipvina, Aug 6, 2011 IP
  6. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #6
    
    <?php
    echo "Total Users :".mysql_num_rows(mysql_query("SELECT * FROM `table_of_members`"));
    ?>
    
    PHP:
    i hav assumed tha u hav connected to the mysql db before u executing this code.
    also change table_of_members to the name of the table which contains users information.
     
    arpit13, Aug 6, 2011 IP
  7. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #7
    What you have above isnt valid syntax - you have "text" inside the PHP tags and then you just have the SQL statement.   You want something like this 
    
    <?php 
    # make sure you connect to the DB first! 
    # See here [url]http://php.net/manual/en/function.mysql-connect.php[/url] 
    # OR [url]http://www.tizag.com/mysqlTutorial/mysqlconnection.php[/url] 
    
    # also check if user is active if valid here 
    
    # The sql query
    $sql = "SELECT count(*) as count FROM users"; 
    
    #Fetch from DB
    $users = mysql_fetch_assoc($sql) or die(mysql_error()); 
    
    # Output here
    
    print "Total Users: " . $users['count']; 
    
    ?>
    PHP:
     
     
    lukeg32, Aug 6, 2011 IP
  8. andreashdk

    andreashdk Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    as told by others its because your code is not valid.

    first select from your database and then count() or mysql_num_row() and then echo that result ;)
     
    andreashdk, Aug 6, 2011 IP
  9. kieranrobo

    kieranrobo Peon

    Messages:
    254
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    whats with all the confusion? arpit13 gave a clear and simple code
     
    kieranrobo, Aug 6, 2011 IP
  10. TwistOffMedia

    TwistOffMedia Well-Known Member

    Messages:
    3,233
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    150
    #10
    Here's what I get: http://likeback.net/users1.php

    Here is the file:

    
    <?php require 'header.php'; ?>
    
    <?php
    # make sure you connect to the DB first!
    # See here [url]http://php.net/manual/en/function.mysql-connect.php[/url]
    # OR [url]http://www.tizag.com/mysqlTutorial/mysqlconnection.php[/url]
    
    # also check if user is active if valid here
    
    # The sql query
    $sql = "SELECT count(*) as count FROM users";
    
    #Fetch from DB
    $users = mysql_fetch_assoc($sql) or die(mysql_error());
    
    # Output here
    
    print "Total Users: " . $users['count'];
    
    ?>
    
    PHP:
    Error on line 14. I have connected to the DB, that's why I included the header file.

     
    TwistOffMedia, Aug 11, 2011 IP
  11. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #11
    It works fine here..... must be your code.

    What error are you getting?
     
    lukeg32, Aug 11, 2011 IP
  12. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #12
    You need to replace this:
    $users = mysql_fetch_assoc($sql) or die(mysql_error());
    PHP:
    with this:
    $users = mysql_fetch_assoc(mysql_query($sql)) or die(mysql_error());
    PHP:
     
    Thorlax402, Aug 11, 2011 IP