Jump to content

[SOLVED] show users online


alohatofu

Recommended Posts

  • Replies 94
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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

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

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.