SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Help With Some Code

    Hi, I have some code running on my website but it seems to be failing some of the time. Basically, this bit of code runs every night to update the website. Could someone have a look over it and suggest where it might be failing or how I could improve it?

    PHP Code:
    $query "SELECT * FROM league_setup WHERE status = 0 AND update_type = 0";
    $getOpenLeagues mysql_query($query);        

    while(
    $row mysql_fetch_assoc($getOpenLeagues))
    {    
        if(
    $row["currentday"] == $row["daysperround"])
        {                                                            
            
    $query "select * from league_rounds where round = ".$row["currentround"]." AND league_ID = ".$row["league_ID"]." ORDER BY runs DESC, wickets ASC";
            
    $getUsers mysql_query($query);
            
            
    $query "select count(user_ID) as numUsers from league_rounds where round = ".$row["currentround"]." AND league_ID = ".$row["league_ID"];
            
    $countUsers mysql_query($query);                
            
    $numUsers mysql_result($countUsers,0,"numUsers");                                                        
                    
            
    $tempRuns 0;
            
    $tempWicks 0;
            
    $count 1;
            while(
    $row1 mysql_fetch_assoc($getUsers))
            {
                if(
    $row1["runs"] == && $row1["wickets"] == 0)
                {
                    
    //Don't do anything!
                
    }                    
                else if(
    $tempRuns == $row1["runs"] && $tempWicks == $row1["wickets"])
                {
                    
    $query "update league_rounds set points = ".($numUsers $count)." where round = ".$row["currentround"]." AND league_ID = ".$row1["league_ID"]." AND user_ID = ".$row1["user_ID"];
                    
    $updateGroups mysql_query($query);
                    
    $count++;                    
                }
                else
                {
                    
    $query "update league_rounds set points = ".$numUsers." where round = ".$row["currentround"]." AND league_ID = ".$row1["league_ID"]." AND user_ID = ".$row1["user_ID"];
                    
    $updateGroups mysql_query($query);
                    
    $count 1;                    
                }

                
    $numUsers--;
                if(
    $row["currentround"] != $row["rounds"])
                {
                    
    $query "insert into league_rounds (user_ID, league_ID, runs, wickets, points, round ) values(".$row1["user_ID"].", ".$row1["league_ID"].", 0, 0, 0, ".($row["currentround"] + 1).")";
                    
    $result mysql_query($query);
                }

                
    $tempRuns $row1["runs"];
                
    $tempWicks $row1["wickets"];
            }                                                                

            if(
    $row["currentround"] == $row["rounds"])
            {
                
    $query "update league_setup set status = 2 where league_ID = ".$row["league_ID"];
                
    $result mysql_query($query);                        
            }
            else
            {
                
    $query "delete from league_chat where league_id = ".$row["league_ID"];
                
    $deleteChat mysql_query($query);    
            
                
    $query "update league_setup set currentday = 1 where league_ID = ".$row["league_ID"];
                
    $result mysql_query($query);
                
                
    $query "update league_setup set currentround = ".($row["currentround"] + 1)." where league_ID = ".$row["league_ID"];
                
    $result mysql_query($query);
            }                                                    
        }
        else
        {
            
    $query "update league_setup set currentday = ".($row["currentday"] + 1)." where league_ID = ".$row["league_ID"];
            
    $result mysql_query($query);
        }            

    I've attached the full file as well.

    Cheers,
    Mute

  2. #2
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right the above code is for a leagues system. Each night, the code will do a few things. If it's not the end of a round, it will add a day to the current day. When the currentday equals the number of days in round (thus the end of the round), the code will give the users in that league their points and start a new round.

    If the current round equals the number of rounds, then the league is over and the code closes the league.

    Runs using a cronjob at midnight, everynight. Last night (and has been happening at othertimes as well) nearly all the leagues closed and those that didn't are now all in round 4 or 5 of a 6 round league.

    Any ideas why the code is failing?

  3. #3
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    sounds like PHP is returning an error code (probably because it takes more than 30 seconds to run your script) and cron is running the script again. if i remember right, by default cron will try to restart a job that ends in error 3 times.

    try adding set_time_limit(0); to the top of your file.

    i would also create a table to record when the over-night process was run with a unique index on the date column. the first query in the script should attempt to insert the current date in to that table. if you get an error, then exit the script. this will prevent cron from running the script multiple times.

    also, until you firgure out whatis causing the script to run multiple times but not complete, you could also convert ALL of the tables involved to innodb, then add mysql_query('begin transaction'); as the second query in the script (i.e., after the query i mention in the previous paragraph) and mysql_query('commit'); as the last query. that way if the script fails half-way through, none of your changes will actually be committed.

    PS: no where in your script are you checking for mysql errors. BAD BAD BAD! FIX IT!

  4. #4
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great points!

    Thanks, I'll try and incorporate that!

  5. #5
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    and i must reiterate:
    Quote Originally Posted by longneck
    PS: no where in your script are you checking for mysql errors. BAD BAD BAD! FIX IT!

  6. #6
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    oh, and it's start transaction or begin. i'm off my game today.

  7. #7
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, got checks in there and so forth now. And yup, it failed. Only ran once though which is good I guess....

    Now I just need to find out where it is failing....


Bookmarks

Posting Permissions

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