Jump to content

[SOLVED] show users online


alohatofu

Recommended Posts

  • Replies 94
  • Created
  • Last Reply

in  your database, make a column for online=0/1... when they login, it switches to 1, when they logout, it switches to 0.

 

then you just...

 

mysql_query("SELECT * FROM `users` WHERE `online`='1'");
.
.
.

 

you'd want to set a timer... just in case they close the browser, without loging out...

Link to comment
Share on other sites

in your login...

mysql_query("UPDATE `user` SET online='1' WHERE id='$id' LIMIT 1"); 

 

in your logout...

mysql_query("UPDATE `user` SET online='0' WHERE id='$id' LIMIT 1");

 

then on every page...

$time=time();
$timeout=time()+(60*60); #now+1hour
mysql_query("UPDATE `user` SET timestamp='$time' WHERE id='$id' LIMIT 1"); 
$query=mysql_query("SELECT * FROM `user` WHERE `online`='1' AND `timestamp`>='$timeout'");
while($row=mysql_fetch_array($query)){
mysql_query("UPDATE `user` SET online='0' WHERE id='$row[id]' LIMIT 1"); 
}

 

code not tested... but it should work  :D

Link to comment
Share on other sites

hi alohatofu,

 

could you please post your comments on wether the code works and what you think. i am actually looking for a very similiar thing to happen on one of my sites.

 

thanks in advance

Link to comment
Share on other sites

Ok,

 

further help is appreciated.

I've altered the table with

ALTER TABLE `surv_member` ADD `online` BINARY NOT NULL ;

 

<?php

$server = "localhost"; // usually localhost
$db_user = "username"; 
$db_pass = "password"; 
$database = "task"; 
mysql_connect($server, $db_user, $db_pass); 

$time=time();
$timeout=time()+(60*60); #now+1hour
mysql_query("UPDATE `username` SET timestamp='$time' WHERE id='$id' LIMIT 1"); 
$query=mysql_query("SELECT * FROM `surv_member` WHERE `online`='1' AND `timestamp`>='$timeout'");
while($row=mysql_fetch_array($query)){
mysql_query("UPDATE `username` SET online='0' WHERE id='$row[id]' LIMIT 1"); 
}

?>

 

but still getting error of

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\sb\index.php on line 531

 

 

Where did I go wrong? Thank you thank you!

Link to comment
Share on other sites

527 $time=time();

528 $timeout=time()+(60*60); #now+1hour

529 mysql_query("UPDATE `username` SET timestamp='$time' WHERE id='$id' LIMIT 1");

530 $query=mysql_query("SELECT * FROM `surv_member` WHERE `online`='1' AND `timestamp`>='$timeout'");

531 while($row=mysql_fetch_array($query)){

532  mysql_query("UPDATE `username` SET online='0' WHERE id='$row[id]' LIMIT 1");

533 }

534

535 ?>

 

Link to comment
Share on other sites

change:

$query=mysql_query("SELECT * FROM `surv_member` WHERE `online`='1' AND `timestamp`>='$timeout'");

 

to:

 

$query=mysql_query("SELECT * FROM `surv_member` WHERE `online`='1' AND `timestamp`>='$timeout'")
or die(mysql_error());

 

This will halt your script and print the mysql error message, report back with the message

Link to comment
Share on other sites

in your while() statement:

 

 print $row['username']; 

 

Because you are selecting every column and fetching them as an array you would use:

$row['columnName']

 

Please mark your thread as resolved if you have no more questions

 

TA!

Link to comment
Share on other sites

Ok guys I would really appreicate this if we can get this done.

 

I still unable to print the result


<?php

$server = "localhost"; // usually localhost
$db_user = "username"; 
$db_pass = "password"; 
$database = "survtask"; 
mysql_connect($server, $db_user, $db_pass); 

$time=time();
$timeout=time()+(60*60); #now+1hour
mysql_query("UPDATE `username` SET timestamp='$time' WHERE memberId='$id' LIMIT 1"); 
$query=mysql_query("SELECT * FROM `surv_member` WHERE `online`='1' AND `timestamp`>='$timeout'")
or die(mysql_error());
while($row=mysql_fetch_array($query)){
mysql_query("UPDATE `username` SET online='0' WHERE memberId='$row[id]' LIMIT 1"); 
$row['online'];
}

//spit out the results
mysql_close();
print "{$row['username']}";

?>

 

It didn't give any error or any result

Link to comment
Share on other sites

in your while statement put

 

print "UPDATE `username` SET online='0' WHERE memberId='$row[id]' LIMIT 1";

 

and check that against your database

 

Google "MYSQL UPDATE STATEMENT" (minus the " 's) for what Syntax to use for UPDATE statements

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • 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.