473,246 Members | 1,495 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,246 software developers and data experts.

Select Distinct Statement Help

I have a question about the following PHP script - I got it off a web
site tutorial on how to count users logged into your site - my
question is the $PHP_SELF variable - it writes the name of the web
page to the 'file' field in the table - I don't understand why it is
doing that - I mean, isn't the SELECT DISTINCT statement only pulling
those records from that one web page? I guess I just don't follow what
it is doing with that SELECT DISTINCT statement???

$timeoutseconds = 300;
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;

$php_SQL = "INSERT INTO online SET timestamp='$timestamp',
ip='$REMOTE_ADDR', file='$PHP_SELF'";
$php_resultID = mysql_query($php_SQL, $php_linkID);

$php_SQL = "DELETE FROM online WHERE timestamp < $timeout";
$php_resultID = mysql_query($php_SQL, $php_linkID);

$php_SQL = "SELECT DISTINCT ip FROM online WHERE file = '$PHP_SELF'";
$php_resultID = mysql_query($php_SQL, $php_linkID);
$php_users_online = mysql_num_rows($php_resultID);

if ($php_users_online == 1)
{
print "<BR>$php_users_online user is currently online";
}
else
{
print "<BR>$php_users_online users are currently online";
}

Jul 16 '05 #1
5 6233

On 18-Aug-2003, Ralph Freshour <ra***@primemail.com> wrote:
I have a question about the following PHP script - I got it off a web
site tutorial on how to count users logged into your site - my
question is the $PHP_SELF variable - it writes the name of the web
page to the 'file' field in the table - I don't understand why it is
doing that - I mean, isn't the SELECT DISTINCT statement only pulling
those records from that one web page? I guess I just don't follow what
it is doing with that SELECT DISTINCT statement???
(snip) $php_SQL = "INSERT INTO online SET timestamp='$timestamp',
ip='$REMOTE_ADDR', file='$PHP_SELF'";
$php_resultID = mysql_query($php_SQL, $php_linkID);

$php_SQL = "DELETE FROM online WHERE timestamp < $timeout";
$php_resultID = mysql_query($php_SQL, $php_linkID); $php_SQL = "SELECT DISTINCT ip FROM online WHERE file = '$PHP_SELF'";
$php_resultID = mysql_query($php_SQL, $php_linkID);
$php_users_online = mysql_num_rows($php_resultID);

(snip)

The script is using $PHP_SELF so that it will work on multiple pages using
the same table. The 'DISTINCT ip' phrase means that SQL will reduce multiple
rows containing the same id to one row. In this case, that means that each
user will only be counted once even though they might have visited the page
many times.

The script inserts the ip address, timestamp, and page ($PHP_SELF) into the
table. It then deletes from the table any rows older than the timeout value.
Next it retrieves the rows for this page from the table while ensuring that
each ip is only represented by one row. Finally, it gets a count of the
number of rows retrieved which is the number of users currently 'online.'


--
Tom Thackrey
www.creative-light.com
Jul 16 '05 #2
Ralph Freshour wrote:
I mean, isn't the SELECT DISTINCT statement only pulling
those records from that one web page? I guess I just don't follow what
it is doing with that SELECT DISTINCT statement???


The WHERE clause makes sure that only records for the current page are
pulled, the "DISTINCT ip" makes sure that each IP adress recorded for
that page is retrieved only once, even if multiple records for that IP
and page exist.

Jochen

--
/**
* @author Jochen Buennagel <zang at buennagel dot com>
* @see http://www.sourceforge.net/projects/zang
*/

Jul 16 '05 #3
That's what I'm referring to in my question, woudn't one want to
'pull' the records for *any* of the web site pages vs just that one
page? Isn't the count incorrect in that it's just count for that one
web page instead of the whole site? I'm thinking to get an accurate
count of who's visiting the site (and not just the specific page) I
need to drop the WHERE clause???
On Wed, 20 Aug 2003 09:27:53 +0200, Jochen Buennagel
<za**@buennagel.com> wrote:
Ralph Freshour wrote:
I mean, isn't the SELECT DISTINCT statement only pulling
those records from that one web page? I guess I just don't follow what
it is doing with that SELECT DISTINCT statement???


The WHERE clause makes sure that only records for the current page are
pulled, the "DISTINCT ip" makes sure that each IP adress recorded for
that page is retrieved only once, even if multiple records for that IP
and page exist.

Jochen


Jul 16 '05 #4
On Tue, 19 Aug 2003 13:55:29 GMT, Ralph Freshour <ra***@primemail.com>
wrote:
On Wed, 20 Aug 2003 09:27:53 +0200, Jochen Buennagel
<za**@buennagel.com> wrote:
The WHERE clause makes sure that only records for the current page are
pulled, the "DISTINCT ip" makes sure that each IP adress recorded for
that page is retrieved only once, even if multiple records for that IP
and page exist.

Jochen
That's what I'm referring to in my question, woudn't one want to
'pull' the records for *any* of the web site pages vs just that one
page? Isn't the count incorrect in that it's just count for that one
web page instead of the whole site? I'm thinking to get an accurate
count of who's visiting the site (and not just the specific page) I
need to drop the WHERE clause???


Sadly, you can't really get an accurate count of people visiting the
site by using IP address. At work, we use a proxy, so we'd all appear
to have the same IP address. At home I dial up to my ISP and could
have a different IP address each time i visit your site.

--
David (please modify address to david@ before replying!)
Jul 16 '05 #5
I see that - then what I will have to do is make use of their login
name - that's unique - and I'll drop the IP address - so then I'll
have an accurate count of users who are logged in which is what I'm
after.

Thanks everyone for the help...!!!
On Tue, 19 Aug 2003 16:10:03 +0100, David Mackenzie
<dc*@tarbrax.freeserve.co.uk> wrote:
On Tue, 19 Aug 2003 13:55:29 GMT, Ralph Freshour <ra***@primemail.com>
wrote:
On Wed, 20 Aug 2003 09:27:53 +0200, Jochen Buennagel
<za**@buennagel.com> wrote:
The WHERE clause makes sure that only records for the current page are
pulled, the "DISTINCT ip" makes sure that each IP adress recorded for
that page is retrieved only once, even if multiple records for that IP
and page exist.

Jochen

That's what I'm referring to in my question, woudn't one want to
'pull' the records for *any* of the web site pages vs just that one
page? Isn't the count incorrect in that it's just count for that one
web page instead of the whole site? I'm thinking to get an accurate
count of who's visiting the site (and not just the specific page) I
need to drop the WHERE clause???


Sadly, you can't really get an accurate count of people visiting the
site by using IP address. At work, we use a proxy, so we'd all appear
to have the same IP address. At home I dial up to my ISP and could
have a different IP address each time i visit your site.


Jul 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Craig Hoskin | last post by:
Hi everyone Have a problem I would areally appreciate help with. I have 3 tables in a standard format for a Bookshop, eg Products Categories Categories_Products the latter allowing me to...
3
by: blue | last post by:
I'm trying to order a varchar column first numerically, and second alphanumerically using the following SQL: SELECT distinct doc_number FROM doc_line WHERE product_id = 'WD' AND doc_type = 'O'...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
9
by: Kelvin | last post by:
Okay so this is baking my noodle. I want to select all the attritbutes/fields from a table but then to excluded any row in which a single attributes data has been duplicated. I.E. Here's my...
2
by: ray well | last post by:
i have to extract info from a legacy access database, which i can't alter, or run APPEND or UPDATE quries against. i can only use SELECT statments to extract what i need. the database has...
8
by: dig314 | last post by:
My goal is to select Vendors that have not had any activity after a given date. The table Orders has all activity from each order. Most vendors will be listed multiple times with activity in...
6
by: Bob Stearns | last post by:
I am getting duplicate rows back from a select distinct statement of the form: SELECT DISTINCT 'jhough', '000111', t0.bhid FROM (SELECT lots of good stuff) t0 LEFT OUTER JOIN another_table ...
5
by: Daniel Wetzler | last post by:
Dear MSSQL experts, I use MSSQL 2000 and encountered a strange problem wqhile I tried to use a select into statement . If I perform the command command below I get only one dataset which has...
2
bergy
by: bergy | last post by:
Hello MS SQL experts, I'm trying to reorganize some data for a friend and I'm running into this problem. Currently he has some duplicate rows that I need to get rid of - only one of the columns has...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.