Jump to content

Recommended Posts

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.mfhosting.com/forum/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--]

Link to comment
https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8889
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8890
Share on other sites

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--]

Link to comment
https://forums.phpfreaks.com/topic/2673-sql-query-is-failing/#findComment-8916
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.