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 ?
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
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.
<?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.
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: Â
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
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.
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: