Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MySQL Error - Is it really this hard?

    Hello!

    I have a script and I am trying to get up time into the database. I dont know why, but when my server is offline, it still adds one. NOTE: all of the other features with offline work, and are entered correctly into the database. Any help would be greatly appreciated.

    Code:
     $query = mysql_query("SELECT * FROM servers") or die(mysql_error()); 
    $servers = array();  
    while($row = mysql_fetch_assoc($query)) {  
     array_push($servers, "{$row['serverIP']}");  
    } 
    
    $query = mysql_query("SELECT * FROM servers") or die(mysql_error()); 
            while($post = mysql_fetch_assoc($query)){ 
                $serverActualUptime = $post['serverActualUptime']; 
                $serverPossibleUptime = $post['serverPossibleUptime']; 
                foreach($servers as $server) { 
                    $stats = \Minecraft\Stats::retrieve(new \Minecraft\Server($server)); 
                    if($stats->is_online){ 
                        $serverStatus = 1; 
                        $serverMOTD = $stats->motd; 
                        $server; 
                        $serverPOnline = $stats->online_players; 
                        $serverMPlayers = $stats->max_players; 
                        $serverVersion = $stats->game_version; 
                        $uptimeClass1 = $serverActualUptime + 1; 
                        $uptimeClass2 = $serverPossibleUptime + 1; 
                        $uptimePercentPart = $uptimeClass1 / $uptimeClass2; 
                        $uptimePercent = $uptimePercentPart * 100; 
                        $queryUpdate = mysql_query("UPDATE servers SET serverStatus='$serverStatus', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers', serverVersion='$serverVersion', serverUptime='$uptimePercent', serverActualUptime='$uptimeClass1', serverPossibleUptime='$uptimeClass2' WHERE serverIP='$server'") or die(mysql_error()); 
                    } else { 
                        $serverStatus = 0; 
                        $serverMOTD = '?'; 
                        $server; 
                        $serverPOnline = '?'; 
                        $serverMPlayers = '?'; 
                        $serverVersion = '?'; 
                        $uptimeClass3 = $serverActualUptime + 0; 
                        $uptimeClass4 = $serverPossibleUptime + 1; 
                        $uptimePercentPart = $uptimeClass3 / $uptimeClass4; 
                        $uptimePercent = $uptimePercentPart * 100; 
                        $queryUpdate = mysql_query("UPDATE servers SET serverStatus='$serverStatus', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers', serverVersion='$serverVersion', serverUptime='$uptimePercent', serverActualUptime='$uptimeClass3', serverPossibleUptime='$uptimeClass4' WHERE serverIP='$server'") or die(mysql_error()); 
                    } 
                } 
            }
    Again, even though the server is offline it is still adding a one to ActualUptime, and I need it added to PossibleUptime only!

    If you can help please do so!

    Thanks,
    TJ

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,426
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Quote Originally Posted by Craytor View Post
    Again, even though the server is offline it is still adding a one to ActualUptime, and I need it added to PossibleUptime only!
    Well that tells you then that there is something wrong here:
    if($stats->is_online){

    Your code will not add 1 to the uptime if that line evaluates as false but if it evaluates as true, it will add one.

    Whatever that line of code is doing, it's not reliable.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •