Jump to content


Photo

sql query is failing


  • Please log in to reply
10 replies to this topic

#1 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 16 October 2005 - 05:20 PM

hi everyone, im running a usersonline script, but it keeps giving me problems really bad, ive asked several times in the php forums, and now its time to ask here, because i think its something to do with the way im using the table in the database,
you can see the script in action here: http://www.spyke01.m...um/indexnew.php

the error im getting is
Error, update query failed UPDATE `usersonline` SET username='spyke01', timestamp='1129482508', ip='71.113.253.198', file='/forum/index.php' WHERE ip='71.113.253.198' OR username='spyke01'

when i run it in phpmyadmin, i get:
#1062 - Duplicate entry '71.113.253.198' for key 1

the database is this:
CREATE TABLE `usersonline` (
  `username` varchar(25) NOT NULL default '',
  `timestamp` int(15) NOT NULL default '0',
  `ip` varchar(40) NOT NULL default '',
  `file` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`ip`)
) TYPE=MyISAM;

the actual code for this page is:
[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]

[span style=\"color:#0000BB\"]<?php
    
    $timeoutseconds [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#DD0000\"]\'2592000\' [/span][span style=\"color:#007700\"]: [/span][span style=\"color:#DD0000\"]\'1200\'[/span][span style=\"color:#007700\"];
    
    [/span][span style=\"color:#0000BB\"]$timestamp[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]time[/span][span style=\"color:#007700\"]();
    [/span][span style=\"color:#0000BB\"]$timeout[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]$timestamp[/span][span style=\"color:#007700\"]+[/span][span style=\"color:#0000BB\"]$timeoutseconds[/span][span style=\"color:#007700\"];
    [/span][span style=\"color:#0000BB\"]$ip [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$REMOTE_ADDR[/span][span style=\"color:#007700\"];
        
    [/span][span style=\"color:#0000BB\"]$currentuser [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]] : [/span][span style=\"color:#DD0000\"]\'Guest\'[/span][span style=\"color:#007700\"];
    
    
    [/span][span style=\"color:#FF8000\"]// Add this user to database
    [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `usersonline` WHERE username = \'\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\' OR ip = \'$ip\'\"[/span][span style=\"color:#007700\"];
    [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]);
            
    if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, add this user
    [/span][span style=\"color:#007700\"]{
        [/span][span style=\"color:#FF8000\"]//not in db, so add them
        [/span][span style=\"color:#0000BB\"]$sql2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"INSERT INTO `usersonline` (username, timestamp, ip, file)\"[/span][span style=\"color:#007700\"].
        [/span][span style=\"color:#DD0000\"]\"VALUES(\'$currentuser\', \'$timestamp\', \'$ip\', \'\"[/span][span style=\"color:#007700\"].[/span][span style=\"color:#0000BB\"]$_SERVER[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'PHP_SELF\'[/span][span style=\"color:#007700\"]].[/span][span style=\"color:#DD0000\"]\"\')\"[/span][span style=\"color:#007700\"];
                      
        [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#DD0000\"]\'<br />Error, insert query failed\' [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]);
    }
    else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script
    [/span][span style=\"color:#007700\"]{
        [/span][span style=\"color:#FF8000\"]//in db, so update their info
        [/span][span style=\"color:#0000BB\"]$sql2 [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#DD0000\"]\"UPDATE `usersonline` SET username=\'$currentuser\', timestamp=\'$timestamp\', ip=\'$ip\', file=\'\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$_SERVER[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'PHP_SELF\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\' WHERE ip=\'$ip\' OR username=\'$currentuser\'\" [/span][span style=\"color:#007700\"]: [/span][span style=\"color:#DD0000\"]\"UPDATE `usersonline` SET username=\'$currentuser\', timestamp=\'$timestamp\', ip=\'$ip\', file=\'\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$_SERVER[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'PHP_SELF\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\' WHERE ip=\'$ip\'\"[/span][span style=\"color:#007700\"];
                      
        [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#DD0000\"]\'<br />Error, update query failed\' [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]);
    }
    [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query
    
    
    // Delete users that have been online for more then \"$timeoutseconds\" seconds
    [/span][span style=\"color:#007700\"]@[/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"delete from useronline where timestamp>$timeout\"[/span][span style=\"color:#007700\"]);
    
    
    [/span][span style=\"color:#FF8000\"]// Shows total users online as well as username
    [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `usersonline`\"[/span][span style=\"color:#007700\"];
    [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]);
    
    if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message
    [/span][span style=\"color:#007700\"]{
        echo [/span][span style=\"color:#DD0000\"]\"\nError reading usersonline table section 2.<br /><br />\"[/span][span style=\"color:#007700\"];
    }
    else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script
    [/span][span style=\"color:#007700\"]{
        [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]);
    }
    [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query
    
    //Find out whos online and if they\'re a user, make a link to their profile page
    [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `usersonline` WHERE username != \'Guest\'\"[/span][span style=\"color:#007700\"];
    [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]);
    
    [/span][span style=\"color:#0000BB\"]$totalguests [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]- [/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]);
    [/span][span style=\"color:#0000BB\"]$totalusers [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]- [/span][span style=\"color:#0000BB\"]$totalguests[/span][span style=\"color:#007700\"];
            
    if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message
    [/span][span style=\"color:#007700\"]{
        [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"No registered users logged in.\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//add username to our list ^^
    [/span][span style=\"color:#007700\"]}
    else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script
    [/span][span style=\"color:#007700\"]{
        while ( [/span][span style=\"color:#0000BB\"]$row [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_array[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) ) {
            [/span][span style=\"color:#0000BB\"]$sql2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `users` WHERE username = \'$row[username]\'\"[/span][span style=\"color:#007700\"];
            [/span][span style=\"color:#0000BB\"]$result2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]);
            
            if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message
            [/span][span style=\"color:#007700\"]{
                [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"No registered users logged in.\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//add username to our list ^^
            [/span][span style=\"color:#007700\"]}
            else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script
            [/span][span style=\"color:#007700\"]{
                while ( [/span][span style=\"color:#0000BB\"]$row2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_array[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]) ) {
                    [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"].= [/span][span style=\"color:#DD0000\"]\"<a href=\'$menuvar[PROFILE]&action=viewprofile&id=\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$row2[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'userid\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\'>$row2[username]</a> \"[/span][span style=\"color:#007700\"];
                }
                
            }
            [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query
        [/span][span style=\"color:#007700\"]}
    
    }
    [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query
    
    //Print out our nice table
    [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#DD0000\"]\"\n<center>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n<table class=\'forumborder\' border=\'0\' cellpadding=\'0\' cellspacing=\'1\' width=\'600\'>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n    <tr class=\'title2\' nowrap height=\'10\'>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n        <td><center><b>Online Stats</b></center></td>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n    </tr>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n    <tr class=\'ncolour2\' height=\'10\'>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n        <td>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Users Online.<br /><br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show total users
    [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$totalguests [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Guests.<br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show number of guests
    [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$totalusers [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Users.<br /><br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show number of users
    [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$users[/span][span style=\"color:#007700\"];    
    echo [/span][span style=\"color:#DD0000\"]\"\n        </td>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n    </tr>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n</table>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n</center><br /><br />\"[/span][span style=\"color:#007700\"];


[/span][span style=\"color:#0000BB\"]?>
[/span]
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 16 October 2005 - 08:02 PM

ip is the primary key. the update query has the condition of OR. so whenever username equals spyke01 (ip doesn't have to match because of OR), ip gets changed. Since it's primary key, you're not allowed to change it to an ip that's already exists.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 16 October 2005 - 10:21 PM

hmm what if i make the timestamp the primary key, will it still fail?

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]

#4 rsnell

rsnell
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 16 October 2005 - 10:34 PM

You trying to update the key field of the table, drop the key = from the update ( but leave it in the where ) and it should work.

#5 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 16 October 2005 - 11:22 PM

well if the key is the ip address, and they log in on another computer, then the ip address wont be correct, wht if i make it so there is no key?

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 16 October 2005 - 11:29 PM

no key would be ok... timestamp can be key but what if two users login the same second?
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 16 October 2005 - 11:29 PM

wht if i make it so there is no key?

I see nothing wrong with that. But why can't you just make the username primary, and then check the username to do the UPDATE?
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#8 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 16 October 2005 - 11:39 PM

you all have made valuable points, the only problem im having is:

if ip is key then it fails
if timestamp is key then it fails if two people do it at the same time
if there is no key then i end up with several entries of the same username
if i use username as the key then the guests im trying to kep track of dont get counted right

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]

#9 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 17 October 2005 - 12:45 AM

can you assign a random (or incremental) number to a guest when they arrive, and append it to the username? Guest123, Guest124.

maybe a separated id field which would be PRIMARY KEY AUTO_INCREMENT, and then change username to UNIQUE, then, using the above method, you can use either one to check for access and updates.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#10 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 17 October 2005 - 01:17 AM

so add say an table olumn named id set to autoincriment then say attach the id value to the end of guest and then change i via ip or username?

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]

#11 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 17 October 2005 - 10:56 PM

ok ive figured it out, mainly what im doing is deleting out the old entries and then adding them again if the user is still online, later on im going to set it up so that it adds/deletes at the loading of the page, that way i can see whos on a certain page and it be correct, heres the source for anyone who needs it, thank for all your help guys!! ^^

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]
[span style=\"color:#0000BB\"]<?php
    
    $timeoutseconds [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#DD0000\"]\'2592000\' [/span][span style=\"color:#007700\"]: [/span][span style=\"color:#DD0000\"]\'1200\'[/span][span style=\"color:#007700\"];
    
    [/span][span style=\"color:#0000BB\"]$timestamp[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]time[/span][span style=\"color:#007700\"]();
    [/span][span style=\"color:#0000BB\"]$timeout[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]$timestamp[/span][span style=\"color:#007700\"]+[/span][span style=\"color:#0000BB\"]$timeoutseconds[/span][span style=\"color:#007700\"];
    [/span][span style=\"color:#0000BB\"]$ip [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$REMOTE_ADDR[/span][span style=\"color:#007700\"];
        
    [/span][span style=\"color:#0000BB\"]$currentuser [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]] : [/span][span style=\"color:#DD0000\"]\'Guest\'[/span][span style=\"color:#007700\"];
    
    [/span][span style=\"color:#FF8000\"]//==========================================================
    // Delete any user entries that match the current user
    // This should take care of multiple user entries
    //==========================================================
    [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]]) ? [/span][span style=\"color:#DD0000\"]\"DELETE FROM `usersonline` WHERE username = \'\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$_SESSION[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'username\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\' OR ip = \'$ip\'\" [/span][span style=\"color:#007700\"]: [/span][span style=\"color:#DD0000\"]\"DELETE FROM `usersonline` WHERE ip = \'$ip\'\"[/span][span style=\"color:#007700\"];
    [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#DD0000\"]\'<br />Error, insert query failed\' [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]);
    
    [/span][span style=\"color:#FF8000\"]//==========================================================        
    // Add this user to database    
    //==========================================================
    [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"INSERT INTO `usersonline` (username, timestamp, ip, file)\"[/span][span style=\"color:#007700\"].
    [/span][span style=\"color:#DD0000\"]\"VALUES(\'$currentuser\', \'$timestamp\', \'$ip\', \'\"[/span][span style=\"color:#007700\"].[/span][span style=\"color:#0000BB\"]$_SERVER[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'PHP_SELF\'[/span][span style=\"color:#007700\"]].[/span][span style=\"color:#DD0000\"]\"\')\"[/span][span style=\"color:#007700\"];
                      
    [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#DD0000\"]\'<br />Error, insert query failed\' [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]);
    
    
    [/span][span style=\"color:#FF8000\"]//==========================================================    
    // Delete users that have been online for more then \"$timeoutseconds\" seconds    
    //==========================================================
    [/span][span style=\"color:#007700\"]@[/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"DELETE FROM `usersonline` WHERE timestamp>$timeout\"[/span][span style=\"color:#007700\"]);
    
        
    [/span][span style=\"color:#FF8000\"]//==========================================================
    // Shows total users online as well as username    
    //==========================================================
    [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `usersonline`\"[/span][span style=\"color:#007700\"];
    [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]);
    
    if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message
    [/span][span style=\"color:#007700\"]{
        echo [/span][span style=\"color:#DD0000\"]\"\nError reading usersonline table section 2.<br /><br />\"[/span][span style=\"color:#007700\"];
    }
    else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script
    [/span][span style=\"color:#007700\"]{
        [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]);
    }
    [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query
        
    //==========================================================
    // Find out whos online and if they\'re a user make a link
    // to their profile page    
    //==========================================================
    [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `usersonline` WHERE username != \'Guest\'\"[/span][span style=\"color:#007700\"];
    [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]);
    
    [/span][span style=\"color:#0000BB\"]$totalguests [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]- [/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]);
    [/span][span style=\"color:#0000BB\"]$totalusers [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]- [/span][span style=\"color:#0000BB\"]$totalguests[/span][span style=\"color:#007700\"];
            
    if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message
    [/span][span style=\"color:#007700\"]{
        [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"No registered users logged in.\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//add username to our list ^^
    [/span][span style=\"color:#007700\"]}
    else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script
    [/span][span style=\"color:#007700\"]{
        while ( [/span][span style=\"color:#0000BB\"]$row [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_array[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]) ) {
            [/span][span style=\"color:#0000BB\"]$sql2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT * FROM `users` WHERE username = \'$row[username]\'\"[/span][span style=\"color:#007700\"];
            [/span][span style=\"color:#0000BB\"]$result2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql2[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]().[/span][span style=\"color:#DD0000\"]\"<br />Couldn\'t execute query: $sql\"[/span][span style=\"color:#007700\"]);
            
            if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]//if NO results, stop the script & return the error message
            [/span][span style=\"color:#007700\"]{
                [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"No registered users logged in.\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//add username to our list ^^
            [/span][span style=\"color:#007700\"]}
            else [/span][span style=\"color:#FF8000\"]//if result found, run the rest of the script
            [/span][span style=\"color:#007700\"]{
                while ( [/span][span style=\"color:#0000BB\"]$row2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_array[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]) ) {
                    [/span][span style=\"color:#0000BB\"]$users [/span][span style=\"color:#007700\"].= [/span][span style=\"color:#DD0000\"]\"<a href=\'$menuvar[PROFILE]&action=viewprofile&id=\" [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$row2[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'userid\'[/span][span style=\"color:#007700\"]] . [/span][span style=\"color:#DD0000\"]\"\'>$row2[username]</a> \"[/span][span style=\"color:#007700\"];
                }
                
            }
            [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query
        [/span][span style=\"color:#007700\"]}
    
    }
    [/span][span style=\"color:#0000BB\"]mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]//free our query
        
    //==========================================================
    //Print out our nice table    
    //==========================================================
    [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#DD0000\"]\"\n<center>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n<table class=\'forumborder\' border=\'0\' cellpadding=\'0\' cellspacing=\'1\' width=\'600\'>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n    <tr class=\'title2\' nowrap height=\'10\'>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n        <td><center><b>Online Stats</b></center></td>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n    </tr>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n    <tr class=\'ncolour2\' height=\'10\'>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n        <td>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#0000BB\"]$totalonline [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Users Online.<br /><br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show total users
    [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$totalguests [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Guests.<br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show number of guests
    [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$totalusers [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\" Users.<br /><br />\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//show number of users
    [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$users[/span][span style=\"color:#007700\"];    
    echo [/span][span style=\"color:#DD0000\"]\"\n        </td>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n    </tr>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n</table>\"[/span][span style=\"color:#007700\"];
    echo [/span][span style=\"color:#DD0000\"]\"\n</center><br /><br />\"[/span][span style=\"color:#007700\"];


[/span][span style=\"color:#0000BB\"]?>
[/span]
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users