alohatofu Posted April 24, 2007 Share Posted April 24, 2007 I have a script that multiple users can logged in. is there a way I can show the current users that are logged in? Quote Link to comment Share on other sites More sharing options...
taith Posted April 24, 2007 Share Posted April 24, 2007 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... Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 could someone please elaborate a little more?  How would I set the timer? and how would I set 1 and 0 in the datatype in mysql? I'm using phpmyadmin  thanks Quote Link to comment Share on other sites More sharing options...
taith Posted April 24, 2007 Share Posted April 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 thanks will try Quote Link to comment Share on other sites More sharing options...
saint959 Posted April 24, 2007 Share Posted April 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 This does not seem to work. Can you please write the code for me to put in the phpmyadmin? I think I didn't create the table correctly. thanks! Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 There is no table creation involved, you have to add the column yourself, dude, GOOGLE IS YOUR FRIEND Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 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! Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 what have you put in line 531? Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 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 ?> Â Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 my database is survtask i have a table surv_member in that table i have username and I just created a column for online with the above table alter Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 Ok so the error is Unknown column 'timestamp' in 'where clause'  so I did  ALTER TABLE `surv_member` ADD `timestamp` TIMESTAMP NOT NULL ;  now there is no error. Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 TADA! Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 thanks that took care of the error but how do I print the username?  I did  mysql_close(); print ("$username");   but it's not showing anything.  help!! almost there Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 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! Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 try <?php mysql_close(); print "{$row['username']}"; ?> Â D'oh shaunrigby wins Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 Because i am GOD! LOL OJ Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 add  mysql_select_db($database);  Underneath your MySQL_Connect function Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 That still didn't change anything. :-( Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 Â that's my database Quote Link to comment Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
alohatofu Posted April 24, 2007 Author Share Posted April 24, 2007 That did not print out anything. Â I checked the UPDATE syntax and they seem to be correct. I guess it does not update the user with a '0' or a '1" correct? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.