What does this error message mean?

Discussion in 'PHP' started by egdcltd, Jan 21, 2007.

  1. #1
    I've been getting this error message, and I've no idea what it means:

    Fatal error: Cannot break/continue 1 level
    Code (markup):
    can anyone tell me please?
     
    egdcltd, Jan 21, 2007 IP
  2. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Without seeing the code segment which is generating the error, this is not easy to diagnose exactly.

    However, are you calling break within an if statement instead of within a loop -- such as for or foreach or while? If so, you will need to restructure your code to resolve the problem.
     
    clancey, Jan 21, 2007 IP
  3. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Break, return and continue can ONLY be used in a for, while or switch statement. If you're wanting to stop the page from processing, try exit or die.
     
    projectshifter, Jan 21, 2007 IP
  4. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here's the code

    function drop_gather_loot($zone_id,$user_id, $type, $skill_number, $tool)
    {
       global $db , $lang, $adr_general;
       $user_id = intval($user_id);
       $zone_id = intval($zone_id);
    
       $adr_user = adr_get_user_infos($user_id);
       $skill_data = adr_get_skill_data($skill_number);
    
       $sql = "SELECT * FROM " . ADR_ZONES_TABLE ."
          WHERE zone_id = $zone_id
          ";
       $result = $db->sql_query($sql);
       if( !$result )
       {
          message_die(GENERAL_ERROR, 'Could not obtain zone information', "", __LINE__, __FILE__, $sql);
       }
       $zone = $db->sql_fetchrow($result);
       
       $zone_loottables = array();
       $zone_loottables = explode(':',$zone['zone_' . $type . '_table']);
    
       $sql = "SELECT * FROM " . ADR_SKILLS_TABLE ."
          WHERE skill_id = 3";
       $result = $db->sql_query($sql);
       if( !$result )
       {
          message_die(GENERAL_ERROR, 'Could not obtain skill information', "", __LINE__, __FILE__, $sql);
       }
       $skill_data = mysql_fetch_array($result);
       
       $skill_to_use = "character_skill_" . $type;
       $user_chance = ( $adr_user[$skill_to_use] * $skill_data['skill_chance']);
       $timer = 0;
       		do
    
    			{
    				$timer++;
    				//roll the loottable
    				$rnd_loottable = rand ( 0 , ( count($monster_loottables) - 1 ));
    				//sort out deactivated loottables
    				$sql = "SELECT * FROM " . ADR_LOOTTABLES_TABLE."
    						WHERE loottable_status = 1
    						AND loottable_id = '".$monster_loottables[$rnd_loottable]."'
    						";
    				$result = $db->sql_query($sql); 
    				if( !$result ) 
    				{ 
    					message_die(GENERAL_ERROR, 'Could not obtain loottable information', "", __LINE__, __FILE__, $sql); 
    				} 
    				//incase all monsters loottables are deactivated for some reason
    				if ($timer > 10000){break;}
    			}
    			while(!$rolled_loottable = $db->sql_fetchrow($result)) ;
    			
    			//incase all monsters loottables are deactivated for some reason
    			if ($timer > 10){break;}
    			
    
    		         //now roll to see if we actually get an item
            		 $max = $user_chance * 100 ;
            		 $dicer = rand ( 1, $max) ;
            		 $dicer = $dicer  + ( $adr_user[$skill_to_use] * $skill_data['skill_chance']);
    			
    			if ($dicer >= $rolled_loottable['loottable_dropchance'])
    			{
    				$sql = "SELECT * FROM " . ADR_SHOPS_ITEMS_TABLE."
    				WHERE item_owner_id = 1
    					AND (item_loottables like '".$rolled_loottable['loottable_id'].":"."%'
    					OR item_loottables like '".$rolled_loottable['loottable_id']."'
    					OR item_loottables like '%".":".$rolled_loottable['loottable_id'].":"."%'
    					OR item_loottables like '%".":".$rolled_loottable['loottable_id']."')
    				";
    				if( !($result = $db->sql_query($sql)) ) 
    				{
    					message_die(GENERAL_ERROR, 'Could not query items list', '', __LINE__, __FILE__, $sql); 
    				}
    				$possible_items_db = $db->sql_fetchrowset($result); 
    				
    				//now roll for the item
    				$rnd_item = rand ( 0 , ( count($possible_items_db) - 1 ));
    				
    				//get the rolled item info
    				$sql = "SELECT * FROM " . ADR_SHOPS_ITEMS_TABLE."
    				WHERE item_owner_id = 1
    					AND item_id = '".$possible_items_db[$rnd_item]['item_id']."'
    				";
    				if( !($result = $db->sql_query($sql)) ) 
    				{
    					message_die(GENERAL_ERROR, 'Could not query items list', '', __LINE__, __FILE__, $sql); 
    				}
    				$rolled_item = $db->sql_fetchrow($result);
    				
    				//new id for the item
    				$sql = "SELECT item_id FROM " . ADR_SHOPS_ITEMS_TABLE ."
    				   WHERE item_owner_id = $user_id
    				   ORDER BY item_id
    				   DESC LIMIT 1";
    				$result = $db->sql_query($sql);
    				if( !$result )
    				{
    					message_die(GENERAL_ERROR, 'Could not obtain item information', "", __LINE__, __FILE__, $sql);
    				}
    				$item_data = $db->sql_fetchrow($result); 
    				
    				$item_id_new = $item_data['item_id'] + 1 ; 
    				$item_type = $rolled_item['item_type_use'] ; 
    				$item_picture = $rolled_item['item_icon'] ; 
    				$item_name = $rolled_item['item_name']; 
    				$item_desc = $rolled_item['item_desc']; 
    				$item_icon = $rolled_item['item_icon'] ; 
    				$item_quality = $rolled_item['item_quality']; 
    				$item_duration = $rolled_item['item_duration']; 
    				$item_duration_max = $rolled_item['item_duration_max']; 
    				$item_add_power = $rolled_item['item_add_power']; 
    				$item_power = $rolled_item['item_power']; 
    				$item_price = $rolled_item['item_price']; 
    				$item_mp_use = $rolled_item['item_mp_use']; 
    				$item_element = $rolled_item['item_element']; 
    				$item_element_str_dmg = $rolled_item['item_element_str_dmg']; 
    				$item_element_same_dmg = $rolled_item['item_element_same_dmg']; 
    				$item_element_weak_dmg = $rolled_item['item_element_weak_dmg']; 
    				$item_max_skill = $rolled_item['item_max_skill']; 
    				$item_weight = $rolled_item['item_weight']; 
    				
    				// Give item to user
    				$sql = "INSERT INTO " . ADR_SHOPS_ITEMS_TABLE . " ( item_id , item_owner_id , item_type_use , item_name , item_desc , item_icon , item_price , item_quality , 
    						item_duration , item_duration_max , item_power , item_add_power , item_mp_use , item_element , item_element_str_dmg , 
    						item_element_same_dmg , item_element_weak_dmg , item_max_skill  , item_weight ) 
    						VALUES ( $item_id_new , $user_id , $item_type , '" . str_replace("\'", "''", $item_name) . "', '" . str_replace("\'", "''", $item_desc) . "' , 
    						'" . str_replace("\'", "''", $item_icon) . "' , $item_price , $item_quality , $item_duration , $item_duration_max , $item_power ,  
    						$item_add_power , $item_mp_use , $item_element , $item_element_str_dmg , $item_element_same_dmg , $item_element_weak_dmg , $item_max_skill , $item_weight)"; 
    				$result = $db->sql_query($sql);
    				if( !$result )
    				{
    					message_die(GENERAL_ERROR, "Item doesn't exist !", "", __LINE__, __FILE__, $sql); 
    				}
    				$dropped_loot_list .= ( $dropped_loot_list == '' ) ? $rolled_item['item_id'] : ":".$rolled_item['item_id'];
    				
    				$message .= "<tr><td align=\"center\"  valign=\"top\">You found a ".adr_get_lang($rolled_item['item_name'])."<br><img src=\"./adr/images/items/".$rolled_item['item_icon']."\"</td></tr>";
    			}
    
         		 // Increases the success uses of this skill and increase level if needed
         		 if ( ( $adr_user['character_skill_mining_uses'] +1 ) >= $skill_data['skill_req'] )
         		 {
            	 $sql = "UPDATE " . ADR_CHARACTERS_TABLE . "
            	    SET character_skill_mining_uses = 0 ,
            	        character_skill_mining = character_skill_mining + 1
    			WHERE character_id = $user_id ";
            	 $result = $db->sql_query($sql);
            	 if( !$result )
            	 {
            	    message_die(GENERAL_ERROR, 'Could not obtain skill information', "", __LINE__, __FILE__, $sql);
            	 }
          		 }
           		else
          		{
            	 $sql = "UPDATE " . ADR_CHARACTERS_TABLE . "
            	    SET character_skill_mining_uses = character_skill_mining_uses + 1
            	    WHERE character_id = $user_id ";
            	 $result = $db->sql_query($sql);
            	 if( !$result )
            	 {
            	    message_die(GENERAL_ERROR, 'Could not obtain item information', "", __LINE__, __FILE__, $sql);
            	 }
          	}
    
       return $message;
    
    }
    PHP:
    The line which is being given in the error report is this one

                if ($timer > 10){break;}
    PHP:
     
    egdcltd, Jan 22, 2007 IP
  5. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You're going to want to use return; on this instead of a break then.
     
    projectshifter, Jan 22, 2007 IP
  6. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The line, if ($timer > 10){break;} is outside the do . . . while loop. You can only use a break inside a loop. You have a similar statement inside the do . . . while loop at if ($timer > 10000){break;}

    What is the maximum number of times you want to loop before breaking? Is it 10? or 10000? or is count($monster_loottables) times?

    From your code, I do not think you want to exit the function if your timer looped more than 10 times in do . . . while loop. I suspect you probably want to comment out the offending line . . . and rethink the loop.
     
    clancey, Jan 22, 2007 IP
  7. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The function is something that originally someone else wrote, but it didn't work. I've been trying to rewrite it. So far, I've managed to get it not to work in completely new ways. I hadn't seen that one line had been duplicated. I'll delete it and see what happens.
     
    egdcltd, Jan 22, 2007 IP
  8. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well that fixed that problem. Now I'm getting an "Item doesn't exist !" message.
     
    egdcltd, Jan 22, 2007 IP
  9. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I always check to make sure values exist before trying to use them and I always preset variables to safe, default values. Your function is assuming that everything is there and not worrying about notice messages. For instance:

    if($user_id) { $user_id = intval($user_id); }
    Code (markup):
    I would also create local versions of variables to deal with these problems. For instance:

    $local_skill_to_use = 0;
    if( isset( $adr_user[$skill_to_use]) )
    { $local_skill_to_use = $adr_user[$skill_to_use]; }
    Code (markup):
    You also assume $monster_loottables is a prepopluated array. I assume this happened someone earlier, but you need to be certain this has occurred. otherwise you cannot count() it or access members.

    I hope this helps.
     
    clancey, Jan 22, 2007 IP
  10. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I think the problem is I'm trying to rewrite something that someone else did that didn't work. I have used some echo statements, although there aren't any currently in it, to see if some stuff was working.

    $monster_loottable was the wrong variable. I'll change it and see what happens (again)
     
    egdcltd, Jan 22, 2007 IP
  11. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #11
    No errors, and it now looks to be working. Not totally sure if it's functioning fully, but at least it's working. Thanks.
     
    egdcltd, Jan 22, 2007 IP