PHP MySQL simple active guests display

Hi,

I've got this which I hoped would show how many active guests are visiting the site in the past hour, it's not working though and I'm not sure why.

Also, I don't know how I would remove users after the hour they should be stored for... I don't want the table filling up forever...

Can anyone offer advice on this? I think several parts maybe flawed.

Thanks.
$d = time() - 60 * 60; //last 1 hour
$q = mysql_query("SELECT username FROM `guests` WHERE time > '$d'");
if (mysql_affected_rows($q) > 0) {
    echo "<ul>";
    while ($users = mysql_fetch_array($q)) {
        echo "<li>{$users[0]}</li>";
    }
    echo "</ul>";
}
if (isset($_SESSION['username'])) {
    $user = $_SESSION['username'];
    $date = time();
    mysql_query("REPLACE INTO `guests` (username,time) VALUES('$user','$date')");
}
else{
    $_SESSION['username'] = $_SERVER["REMOTE_ADDR"];
    $user = $_SESSION['username'];
    $date = time();
    mysql_query("INSERT INTO `guests` (username,time) VALUES('$user','$date')");
}

Open in new window

LVL 6
i-CONICAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZylochCommented:
Please try replacing mysql_affected_rows() with mysql_num_rows(). From the manual, the former does not apply to SELECT statements, while the latter does.

As to periodically pruning users, you might consider running a PHP script using a cron job that does this. Another way to do this without cron is to include code on the page so that every time a page is loaded there is a small probability for the pruning code to run. This should keep speed quick enough for users most of the time.

Alternatively, one avenue you may pursue is some way to do this automatically in the database (depending on your database privileges). However, this is not my area of expertise, so I can't tell you if or how this is possible.
0
adbrettCommented:
try:
<?php

$one_hour = 60 * 60;
$now = time(); // set time to a varaible so we don't have to keep calling the function
$time_ago = $now - $one_hour; // it's best to use verbose varaible names so it reads well in english


$query = mysql_query("SELECT username FROM `guests` WHERE time > '{$time_ago}'"); // same as yours

if (mysql_num_rows($query) > 0) { // affected rows only works on update/insert/replace/delete, so use num_rows for a query
    echo "<ul>";
    while ($users = mysql_fetch_array($query)) {
        echo "<li>{$users[0]}</li>";
    }
    echo "</ul>";
}

// removed call to time() in both if statements to save on function calls
// moved mysql_query outside if statement to save on code repetition
if (!empty($_SESSION['username'])) {
    $user = $_SESSION['username'];
}
else{
    $user = $_SERVER["REMOTE_ADDR"]; // there's no point setting $_SESSION['username'] here unless you did it for some other reason
}

mysql_query("INSERT INTO `guests` (username, time) VALUES('{$user}', '{$now}')"); // insert date & username into your table
mysql_query('DELETE FROM `guests` WHERE time < {$time_ago}'); // delete anything over 1 hour ago to keep the table clean

?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
i-CONICAAuthor Commented:
Thanks so much guys. Really appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.