0

I am facing a Peculiar behavior or you can say ghostly attitude of for loop in php. I am retrieving 10000 products using an API. After that I am inserting those products in my Database.Codes are working properly in localhost but creating problem in production server.I am describing it below.

$value_array = array();        
$counter=0;                
$time_start = microtime(true); 

for($j=0;$j<count($result);$j++)
{   
   if(isset($result->Item[$j]->itemID))
   {            
      $value_array[] = (int)$result->Item[$j]->itemID.','.(int)$result->Item[$j]->systemSku;  
      $counter++;
   }
   else
   {
       var_dump($result);
       die();                   
   }


   if($counter == 100)
   {
       echo $counter;
       $time_end = microtime(true);
       $execution_time = ($time_end - $time_start);
       echo '<br /><b>Total Execution Time:</b> '.$execution_time.' Sec';

       die();
   }   
} 

Above code create output like below

100
Total Execution Time: 39.075633049011 Sec

But if I change the code like below, I am getting a white page (No output).

    $value_array = array();        
    $counter=0;                
    $time_start = microtime(true); 

    for($j=0;$j<count($result);$j++)
    {   
       if(isset($result->Item[$j]->itemID))
       {            
          $value_array[] = (int)$result->Item[$j]->itemID.','.(int)$result->Item[$j]->systemSku;  
          $counter++;
       }
       else
       {
           var_dump($result);
           die();                   
       }


       if($counter == 9000)  //I changed the code here 
       {
           echo $counter;
           $time_end = microtime(true);
           $execution_time = ($time_end - $time_start);
           echo '<br /><b>Total Execution Time:</b> '.$execution_time.' Sec';

           die();
       }   
    } 

I changed the code like below again.

$value_array = array();        
$counter=0;                
$time_start = microtime(true); 

for($j=0;$j<count($result);$j++)
{   
  if(isset($result->Item[$j]->itemID))
  {            
    $value_array[] = (int)$result->Item[$j]->itemID.','.(int)$result->Item[$j]->systemSku;  
    $counter++;
  }
  else
  {
    var_dump($result);
    die();                   
  }

  echo $counter.'<br/>';  //I changed the code here

   if($counter == 9000)   
   {
      echo $counter;
      $time_end = microtime(true);
      $execution_time = ($time_end - $time_start);
      echo '<br /><b>Total Execution Time:</b> '.$execution_time.' Sec';

      die();
     }   
   }

I am getting output like below

all counter values
9000
Total Execution Time: 562.96646595001 Sec

What can be the problem ?? Why the first code behaving weird ??

julious ceazer
  • 331
  • 2
  • 4
  • 14
  • 2
    I'd have suggested a probable timeout – Mark Baker Apr 30 '15 at 09:30
  • Why retrieve 10k records and then insert into db? insert per record...? sometimes is seems more of a hassle, but much easier to troubleshoot, because (if) it will break on a record, you can see the values for that particular record, instead of sorting through thousands of records. one record set can stop a query also. so your problem can be in other places too, eg. in transporting table structure. – csaw Apr 30 '15 at 09:41
  • Thanks @csaw for your response. **insert per record** needs more time than insert all record using one query. _Thanks_ – julious ceazer Apr 30 '15 at 09:49

1 Answers1

3

I looks like a time limit on the script running is only enforced when no output is generated for a long time, so echoing something at regular intervals helps it finish. But that is just speculation.

I would start by checking for differences in the PHP settings of the two servers, maybe the max_execution_time setting.

You can check this using phpinfo()

VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63
Totoro53
  • 338
  • 4
  • 17
  • 1
    Thanks @Totoro53 for your reply. This code is running good in localhost where **max_execution_time** is **500000**. I changed my Server configuration as **max_execution_time** is **500000**, but output is same as before. _Thanks_ – julious ceazer Apr 30 '15 at 10:44
  • 1
    Do you have display errors enabled on the server that produces the white page? If not, I suggest you do so at least temporarily as it may shed more light on what is going wrong. http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Totoro53 Apr 30 '15 at 22:01
  • Thanks @Totoro53 for your reply. I used these codes `ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1);` I also used below codes in **php.ini** file display_errors = on display_startup_errors = on But the situation is unchanged so far. – julious ceazer May 04 '15 at 08:35