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

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

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!

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 ?>

 

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

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

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.