473,769 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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='$tim estamp',
ip='$REMOTE_ADD R', file='$PHP_SELF '";
$php_resultID = mysql_query($ph p_SQL, $php_linkID);

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

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

if ($php_users_onl ine == 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 6262

On 18-Aug-2003, Ralph Freshour <ra***@primemai l.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='$tim estamp',
ip='$REMOTE_ADD R', file='$PHP_SELF '";
$php_resultID = mysql_query($ph p_SQL, $php_linkID);

$php_SQL = "DELETE FROM online WHERE timestamp < $timeout";
$php_resultID = mysql_query($ph p_SQL, $php_linkID); $php_SQL = "SELECT DISTINCT ip FROM online WHERE file = '$PHP_SELF'";
$php_resultID = mysql_query($ph p_SQL, $php_linkID);
$php_users_onli ne = 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***@primemai l.com>
wrote:
On Wed, 20 Aug 2003 09:27:53 +0200, Jochen Buennagel
<za**@buennage l.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.fr eeserve.co.uk> wrote:
On Tue, 19 Aug 2003 13:55:29 GMT, Ralph Freshour <ra***@primemai l.com>
wrote:
On Wed, 20 Aug 2003 09:27:53 +0200, Jochen Buennagel
<za**@buennag el.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
45467
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 have products in multiple categories.
3
10561
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' ORDER BY CASE WHEN IsNumeric(doc_number) = 1 THEN CONVERT(FLOAT, doc_number) ELSE 999999999 END,
3
6472
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 COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
9
10905
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 table:- ID Ref Name DATE 1 AAA Joe 1/2 2 BBB Ken 1/2 3 AAA Len 6/3
2
1470
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 multiple entries for the same first and last name, i need to generate a dataset that has no duplicate first and last names. the sql statment itself is quite easy SELECT DISTINCT tblNameAndAddress.LastName, tblNameAndAddress.FirstName
8
1272
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 several years. Below is my Select statement: SELECT Distinct Vendor FROM Orders WHERE update_date < #01/01/2002#
6
13764
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 t1 ON relevant_stuff WHERE (lots of conditions) After re-reading the relevant pat ofVol 1 of the SQL Reference I am unablee to see how this is possible.
5
6898
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 the described properties. If I use the same statement in a select into statement (see the second select) I get several datasets with the described properties like I didn't use distinct
2
3078
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 duplicates and then there is a unique primary key. The table looks like this: id color --- ------ 3 red 4 red 5 blue 6 green
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.