473,378 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Trying to ad a online table, what should it be set to?

117 100+
Ok, i am adding a table to the users table called online.
So when they login it will set (online) to Y and when they logout it will be set to N.

But what should it be created as, it will be updated often and i need to beable to read it from my php in the Members online section?
varchar(50), int(10), or Text...

And how do i call the results of it not in text Y but in total.
Like lets say there is a total 50 users=Y so it will PRINT 50.

I have tried several times but i keep getting eather 9empty) or (Y) Not the total as 8

I created the table like this:
[PHP]ALTER TABLE users
ADD online varchar(1) default 'N';[/PHP]

And call it like this:
[PHP]$sql="SELECT online FROM users WHERE online = $Y";
$result = mysql_query($sql ,$db);[/PHP]

And call it:
[PHP]<?php printf($online); ?>[/PHP]

is there a special way to count the value of Y and skip N
Aug 29 '07 #1
21 1758
ak1dnar
1,584 Expert 1GB
Ok, i am adding a table to the users table called online.
So when they login it will set (online) to Y and when they logout it will be set to N.

But what should it be created as, it will be updated often and i need to beable to read it from my php in the Members online section?
varchar(50), int(10), or Text...
Still I am struggling to understand your question Breana. could you please explain it little bit clearly. Thanks!
Aug 29 '07 #2
Breana
117 100+
Ok, i am doing this.
ALTER TABLE users
ADD online (.........) ;

But what do i set it to?
Text, INT(10) , VARCHAR (50) or else?

I need to be able to read from it and update it easly.
Default has to be 'n'

So i tried this:
ALTER TABLE users
ADD online char(1) default 'n';
Aug 29 '07 #3
ak1dnar
1,584 Expert 1GB
And how do i call the results of it not in text Y but in total.
Like lets say there is a total 50 users=Y so it will PRINT 50.

I have tried several times but i keep getting Y,Y,Y,Y,Y,Y,Y,Y Not the total as 8
Now you have edited the original post with more details, but yet it doesn't describe the problem. Sorry...Could you please show us the NOT working code snippets. then it might be easier to get through it.
Aug 29 '07 #4
ak1dnar
1,584 Expert 1GB
Ok, i am doing this.
ALTER TABLE users
ADD online (.........) ;

But what do i set it to?
Text, INT(10) , VARCHAR (50) or else?

I need to be able to read from it and update it easly.
Default has to be 'n'

So i tried this:
ALTER TABLE users
ADD online char(1) default 'n';
Create a separate column (Say Status) on the users table where you have stored those login details. and set the default value to the column as 'N'.
Then when users logs in to the system UPDATE the records with Setting up the online status with 'Y'.

data type for the Status column should be varchar(1).
Aug 29 '07 #5
Breana
117 100+
I did that, i updated my first post.
But i cant call it, it just shows y
Aug 29 '07 #6
pbmods
5,821 Expert 4TB
Heya, Breana.

Here's an idea which will let you see what Users are logged in and simultaneously enforce page timeout.

Instead of 'Y/N', use a timestamp:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE
  2.         `users`
  3.     ADD
  4.         `last_active`
  5.             TIMESTAMP
  6.             NOT NULL
  7.             DEFAULT CURRENT_TIMESTAMP
  8.             ON UPDATE CURRENT_TIMESTAMP,
  9.     ADD
  10.         KEY
  11.             (`last_active`);
  12.  
Then, set and enforce your default timeout, and if the User's session is still valid, 'touch' his `last_active` value:
Expand|Select|Wrap|Line Numbers
  1. // Check to see if User's session has timed out.
  2. $_sql = "
  3. SELECT
  4.         `last_active`
  5.     FROM
  6.         `users`
  7.     WHERE
  8.         `user_id` = '{$user_id}'
  9.     LIMIT 1";
  10. $_res = mysql_query($_sql);
  11. $_row = mysql_fetch_row($_res);  //  $_row[0] is `last_active`.
  12. mysql_free_result($_res);
  13.  
  14. // Set timeout to 15 minutes
  15. define('TIMEOUT', 900);
  16. if( strtotime($_row[0]) < (time() - TIMEOUT) )
  17. {
  18.     // User is no longer logged in; his session has timed out
  19. }
  20. else
  21. {
  22.         // User is logged in; set his `last_active` to CURRENT_TIMESTAMP.
  23.     $_sql = "
  24. UPDATE
  25.         `users`
  26.     SET
  27.         `last_active` = NOW()
  28.     WHERE
  29.         `user_id` = '{$user_id}'
  30.     LIMIT 1";
  31.     mysql_query($_sql);
  32. }
  33.  
Then, to find the number of Users that are online (active within the last 15 minutes):
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(`user_id`)
  3.     FROM
  4.         `users`
  5.     WHERE
  6.         `last_active` > TIMESTAMP(NOW(), '00:15:00')
  7.     GROUP BY
  8.         `user_id`
  9.  
Aug 29 '07 #7
Breana
117 100+
Ok, seems complex but how do i set there "CURRENT_TIMESTAMP" in the login or is it auto done?
Aug 29 '07 #8
pbmods
5,821 Expert 4TB
Heya, Breana.

CURRENT_TIMESTAMP is an alias for NOW(). It's part of the 'magical' properties in MySQL timestamps.

It looks complex, but in reality, it's not that different than what you are doing now; instead of using 'y'/'n', you are using a number that represents the last time the User did anything.

Instead of checking to see if `online` is 'y', you are checking to see if `online` is within the last 15 minutes.

Your call. I had fun writing the timestamp solution at any rate*, so don't worry about my feelings if you want to go with the 'y'/'n' column anyway :)

*actually, you forced me to take a critical look at an odd 'feature' of my code. Right now, my session frameworks store the timeout data in the _SESSION instead of in the database. I really should be fixing this! Thanks for the inspiration!
Aug 29 '07 #9
Breana
117 100+
So do i set the the table like this..

[PHP]$sql = "UPDATE last_active from users SET last_active >= ($now - $timeout)";
$result = $this->_query($sql);[/PHP]

Like that?
Aug 29 '07 #10
pbmods
5,821 Expert 4TB
Heya, Breana.

You've managed to create a UPDECT query, which is a cross between a SELECT and an UPDATE. It's not valid, but it is very creative.

The idea is that the `last_active` field stores the time of last activity; that is, the time that the User loaded the page. So you can simply set its value to the current datetime when updating it:
Expand|Select|Wrap|Line Numbers
  1.  $_sql = "
  2. UPDATE
  3.         `users`
  4.     SET
  5.         `last_active` = NOW()
  6.     WHERE
  7.         `user_id` = '{$user_id}'
  8.     LIMIT 1";
  9.  
Aug 29 '07 #11
Breana
117 100+
I tried to but it dont change, allways 000-000-000
Iposted my code maybe i placed it wrong..

[PHP]<?php

$sql = "select * from users where login = '$login' and password = '$password'";
$result = mysql_query($sql ,$db);

if ($myrow = mysql_fetch_array($result)) {

do {

$uid = $myrow["userid"];
$uname = $myrow["login"];

} while ($myrow = mysql_fetch_array($result));

$loggedin = true;
$upwd = $password;
$msg = "<table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td><img src=\"images/top_c.gif\" width=\"500\" height=\"24\"></td>
</tr>
<tr>
<td align=\"center\" background=\"images/b_m.gif\">Welcome <font color=\"#FF0000\">$uname</font>, you are now logged in.</td>
</tr>
<tr>
<td><img src=\"images/bottom_c.gif\" width=\"500\" height=\"24\"></td>
</tr>
</table><br />
<br />
<a href=\"index.php\">CONTINUE >></a><br /><br /><p align=\"center\"><img src=\"images/Welcome_Back.gif\" width=\"300\" height=\"282\" /></p>";
$sql = "UPDATE `users` SET `last_active` = NOW() WHERE `user_id` = '{$user_id}' LIMIT 1";

} else {
$loggedin = false;
$upwd = "";
$uid = "";
$uname = "";
$msg = "<img src=\"images/invalid.gif\" width=\"402\" height=\"107\" /><br /><b><font color=\"#FF0000\">Sorry,</font></b> that login and password is not valid.<br /><br />If you have forgotten your password <a href=forgot.php>Reset Password</a>. <br />If you are a new user you will need to <a href=newuser.php>Create A New Account!</a>";


}

session_register("loggedin");
session_register("upwd");
session_register("uid");
session_register("uname");

?>[/PHP]
Aug 29 '07 #12
pbmods
5,821 Expert 4TB
Heya, Breana.

Make sure that the `last_active` field is a TIMESTAMP with DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP.

Also note this line:
Expand|Select|Wrap|Line Numbers
  1.  $sql = "UPDATE `users` SET `last_active` = NOW() WHERE `user_id` = '{$user_id}' LIMIT 1";
  2.  
You'll want to replace $user_id with $uid, since that's the variable that stores the User's ID. Also, in your Users table, the User ID field is userid, not user_id. I just put some generic names in there.

Your code should look more like this:
Expand|Select|Wrap|Line Numbers
  1.  $sql = "UPDATE `users` SET `last_active` = NOW() WHERE `userid` = '{$uid}' LIMIT 1";
  2.  
Aug 29 '07 #13
Breana
117 100+
Oh ok, i did it now error or blank page, but on the part where i posted the whos online is empty?

I used this to call it:
[PHP]Total online: <?
$_sql = "SELECT
COUNT(`uid`)
FROM
`users`
WHERE
`last_active` > TIMESTAMP(NOW(), '00:15:00')
GROUP BY
`uid`" ?>[/PHP]
Aug 29 '07 #14
pbmods
5,821 Expert 4TB
Heya, Breana.

You're setting the value of $_sql, but you never call mysql_query()....
Aug 29 '07 #15
Breana
117 100+
Ok like this:
[PHP]<?
$sql = "SELECT COUNT(`uid`) FROM `users` WHERE `last_active` > TIMESTAMP(NOW(), '00:15:00')
GROUP BY `uid`";
$result = mysql_query($sql ,$db);?>[/PHP]
Aug 29 '07 #16
pbmods
5,821 Expert 4TB
Heya, Breana.

That looks pretty good. Then just call mysql_fetch_row($result), and echo index 0 of the resulting array.
Aug 29 '07 #17
Breana
117 100+
Ok i have it like this:
[PHP]Total online: <?
$sql = "SELECT COUNT(`uid`) FROM `users` WHERE `last_active` > TIMESTAMP(NOW(), '00:15:00')
GROUP BY `uid`";
$result = mysql_query($sql ,$db);
mysql_fetch_row($result);?>[/PHP]

"echo index 0" what....
you mean i need to echo ("index 0"); or what?
Aug 29 '07 #18
pbmods
5,821 Expert 4TB
Heya, Breana.

This line:
Expand|Select|Wrap|Line Numbers
  1. mysql_fetch_row($result);
  2.  
Returns an array. You'll want to assign that array to a variable and then echo its 0th index.

I.e.,
Expand|Select|Wrap|Line Numbers
  1. $row = mysql_fetch_row($result);
  2. echo $row[0];
  3.  
Aug 29 '07 #19
Breana
117 100+
Ok now it works sort of, it don't show 1 or 5 but this 209.33.95.33?
What is that... my ip address. (0 ^ 0)

Do i need to change the (0) to uid? so it shows a number
Aug 29 '07 #20
Breana
117 100+
I just tried again i cant seem to get the number to pop not the ip or what ever that is...
Aug 29 '07 #21
Breana
117 100+
Anyone got an idea on how to fix this?
Aug 30 '07 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Lara | last post by:
Hello freaks, we have many problems with our online reorg and no idea how to resolve it. We had to do an online reorg beacause of 24 h online business. We start the reorg-statements (table by...
16
by: andy.standley | last post by:
Hi, we are running DB2 V8.2 (8.1.0.80) on redhat linux and I am trying to set the reorg to be online. I use the control center on the box - db2cc and then configure automatic maintenance wizard -...
12
by: Ron Weldy | last post by:
I have a test server runinng 2003/IIS 6 with a mixture of asp and asp.net files. On my workstation I have a share set up to the folder where the web files reside. I am just doing quick and dirty...
3
by: Lada 'Ray' Lostak | last post by:
Hello ppl, Imagine some online system, based on PgSql. There are 'many' parts which depends on each other. I will give small example, but instead of simple table imagine changing tenths various...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
9
by: Jeremy | last post by:
I have a situation where i am trying to run two query's and retrieve one record from each query, then using Union combine them into one recordset. The First Query is strait forward and should just...
4
by: Jeff | last post by:
I've been attempting to find the answer, but don't know what to search for. If someone could get me started, I can figure the rest out. I'm using visual web 2005 with vb. The following works...
1
by: dn.perl | last post by:
I have a table t1 with two columns : c11 varchar(32) , c22 varchar(32) The data in the table is : '11', 'aa01' and on upto '11', 'aa50' : total 50 entries '22', 'b01' '22', b'02' '22',...
6
by: michael.spoden | last post by:
Hi, how can I fix lock-waits during an online backup? Is an online backup in DB2 V8.2 not realy online? I'm using DB2 V8.2 Fixpak 15 on Linux. The command to perform the backup is: db2 backup...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.