Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/196866-this-is-failing-for-some-reason/
Share on other sites

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

 

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.

 

 

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?

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.