TeddyKiller Posted March 29, 2010 Share Posted March 29, 2010 I have something that runs ever second via javascript, however.. it doesn't update the database if the second difference is bigger than 10, although it shouldn't be, theres no reason for it to be like that. The php is.. $query = mysql_query("SELECT * FROM chat_users"); $row = mysql_fetch_array($query); if(($row['time_active'] + 10) < time()) { $query = mysql_query("DELETE FROM `chat_users` WHERE `user_id` = '".$row['user_id']."'"); } It works when the users are newly offline, but if they've been offline for hours, it doesn't work. (When someone logs on for the query to run that is, it doesn't remove them from the database) Any reason why? Quote Link to comment https://forums.phpfreaks.com/topic/196866-this-is-failing-for-some-reason/ Share on other sites More sharing options...
ignace Posted March 29, 2010 Share Posted March 29, 2010 Please post your code in its entirety Quote Link to comment https://forums.phpfreaks.com/topic/196866-this-is-failing-for-some-reason/#findComment-1033530 Share on other sites More sharing options...
TeddyKiller Posted March 29, 2010 Author Share Posted March 29, 2010 Okay.. although I don't suppose anything else is part the problem. <?php session_start(); include("inc/config.php"); include("inc/functions.php"); $user = check_user($secret_key); //Check to ensure the user is in a chat room. if(isset($_GET['chat'])) { $query = mysql_query("SELECT user_name FROM chat_users WHERE user_id = " . $user->id . ""); //If the user isn't in the database, there for insert them. if(mysql_num_rows($query) < 1) { $date = time(); $query = mysql_query("INSERT INTO `chat_users` (user_id, user_name, time_active) VALUES ('$user->id', '$user->username', '$date')") or die(mysql_error()); } $query = mysql_query("SELECT * FROM chat_users"); $row = mysql_fetch_array($query); //If timestamp hasn't been updated for atleast 10 seconds. if(($row['time_active'] + 10) < time()) { //They are not in chat, so delete them $query = mysql_query("DELETE FROM `chat_users` WHERE `user_id` = '".$row['user_id']."'"); } //Update theu sers time_active $date = time(); $query = mysql_query("UPDATE `chat_users` SET `time_active`='$date' WHERE `user_id` = $user->id"); //Get new updates (If a user was removed) $query = mysql_query("SELECT user_name FROM chat_users"); $users = ''; while($row = mysql_fetch_array($query)) { $users .= '* '.ucwords($row['user_name']).'<br />'; } } //Echo the users echo $users; ?> Quote Link to comment https://forums.phpfreaks.com/topic/196866-this-is-failing-for-some-reason/#findComment-1033537 Share on other sites More sharing options...
andrewgauger Posted March 29, 2010 Share Posted March 29, 2010 Why don't you set it up so *regardless* what user is logged on, it deletes ALL users that have "expired". $old=time()-10; DELETE FROM `chat_users` WHERE time_active < $old; That way you can have your active users kill your inactive users. Quote Link to comment https://forums.phpfreaks.com/topic/196866-this-is-failing-for-some-reason/#findComment-1033553 Share on other sites More sharing options...
TeddyKiller Posted March 29, 2010 Author Share Posted March 29, 2010 but what if the last person was logged on, and then logged off.. they'll be nobody to run the query to remove them from the database? So in that way they've exceeded the time()-10, there for it wont delete them? edit: Oh wait, I didn't see the < . I'll try that out unless anyone had any better ways? Quote Link to comment https://forums.phpfreaks.com/topic/196866-this-is-failing-for-some-reason/#findComment-1033564 Share on other sites More sharing options...
andrewgauger Posted March 29, 2010 Share Posted March 29, 2010 So the last person will exist in the database until the next person logs in. Is that a problem? No one is logged in to see them. Quote Link to comment https://forums.phpfreaks.com/topic/196866-this-is-failing-for-some-reason/#findComment-1033679 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.