473,395 Members | 1,471 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,395 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 6237

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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.