Whos online script help???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Whos online script help???

Post by Joe »

I am currently working on a who's online script but i have a problem. The method I am using is when a user logs in it adds 1 to the database column then the index page adds up all of the values and shows the total. The script is as follows:

<?php
$link = mysql_connect("???", "???", "???");
mysql_select_db("???") or die("Could not connect!" . mysql_error());

if (mysql_num_rows($result))
{
$row = mysql_fetch_assoc($result);
$users = $row['active'];
mysql_query("SELECT add($users) FROM online") or die(mysql_error());
echo "<center>(There are ".$users." Currently online)</center>";
}
?>

Regards


Joe 8)
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Whos online script help???

Post by TheBentinel.com »

Joe wrote:I am currently working on a who's online script but i have a problem. The method I am using is when a user logs in it adds 1 to the database column then the index page adds up all of the values and shows the total.
What problem are you having? Is the counter not getting decremented when they log out, presumably because they don't actually log out, they just stop surfing or go somewhere else?

I think those "who's online right now" scripts usually are based on the time since the user was last active. So every time the user visits your site, you write a last-access time-date stamp to the user's profile. Then you pull up a list of everyone that has accessed your site in the past 5 minutes and call them "active". They may not be, they may have left, but there's no reliable way for you to know that. The time stamp is a reasonable guess.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I have got all the timing sorted out and the works perfect. Its just the fact that it does not show how many users are actually online when i use this part:

echo "<center>(There are ".$users." Currently online)</center>";

I just get a plain old error. Please help

Regards


Joe 8)
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

What is your error message that you are getting?

In the mean time, try to put double quotes, " , around active in the line:

Code: Select all

$users = $row['active'];
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Whos online script help???

Post by TheBentinel.com »

Joe wrote:

Code: Select all

<?php
  $link = mysql_connect("???", "???", "???");
  mysql_select_db("???") or die("Could not connect!" . mysql_error());
 
 if (mysql_num_rows($result))
 {
  $row = mysql_fetch_assoc($result);
  $users = $row['active'];
  mysql_query("SELECT add($users) FROM online") or die(mysql_error());
  echo "<center>(There are ".$users." Currently online)</center>";
 }
?>
Agreed, we need the exact text of the error message to help you most.

Meanwhile, though, what is the mysql_query doing for you? Does it really populate the $users variable? I thought it would return a recordset that you would need to read through.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

try this:

Code: Select all

$res = mysql_query("SELECT add($users) as users FROM online") or die(mysql_error());
$res = mysql_fetch_assoc($res);
$users = $res['users'];
echo "<center>(There are $users Currently online)</center>";
Note: i haven't tested this actuall code, but im almost positive it'll work!!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Sorry Joe, but that piece of code just doesn't look right.

Get ideas from here.
viewtopic.php?t=12980
Post Reply