Jump to content

Count DB


avatar.alex

Recommended Posts

ok, firstly, if each user is on record in a table, then simply go SELECT User_ID FROM Table

 

then go mysql_num_rows.  thats the number of users,

 

almost the same for onlin, simply have a field, that when a user logs in, it writes to that field, "logged in" or something, and go

 

SELECT User_ID FROM Table WHERE Logged_In = 'logged in'

 

then mysql_num_rows

 

if you give a bit of code, ill do a quick example if you need

 

 

gdlk

Link to comment
Share on other sites

the table that the users are stored is called bl_admin I don't know if you need a field but I think its all

 

  adminid  bigint(20)  No    auto_increment             

  username  varchar(255)  No                 

  password  varchar(255)  No                 

  status  int(11)  No  3               

  name  varchar(255)  No                 

  validated  int(11)  No  0               

  keynode  varchar(255)  No                 

  email  varchar(255)  No                 

  css  longtext  No                 

  lastposttime  bigint(20)  No  0               

  bannedfromforum  int(11)  No  0               

  lasttime  bigint(20)  No  0               

  tsgone  bigint(20)  No  0               

  oldtime  bigint(20)  No  0 

 

Link to comment
Share on other sites

To count the number of users in the database you could run a query like this:

 

$result= mysql_query("SELECT COUNT(*) FROM bl_admin")

 

... and then show it like this ...

 

echo 'Total Users: '. mysql_result($result, 0);

 

To show the number of users online, you could first set up a field in your bl_admin table that would track the last time a user performed an action (perhaps this is what your lasttime field is for?). The easiest way would be to have code included on each page that would update that information on each page reload.

 

I'll assume you are using sessions to track users that are logged in.

 

Something like:

 

if($_SESSION['logged_in']){
$sql = "UPDATE bl_admin SET lasttime = NOW() WHERE username = '$username';
mysql_query($sql) or die(mysql_error());
}

 

... then to show the users that are online, you'll need to set a "timeout" value. This will set a period of inactivity that will determine if a user is no longer online.

 

$timeout = 300; // in seconds

// Get a timestamp for the timeout time
$timestamp_limit = time() - $timeout;

// Create and run the query
$result = mysql_query("SELECT username FROM bl_admin WHERE UNIX_TIMESTAMP(lasttime) > $timestamp_limit;
$result_numrows = mysql_num_rows($result);
$result_array = mysql_fetch_array($result);

for($i=0;$i < $result_numrows;$i++){
if($i == 0){
	echo '# of Users Online: '.$result_numrows.'<br>';
	echo 'Users Online: ';
}

echo $result_array['username'].',';
}

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.