title: PHP/SQL script stops unexpectedly, works in debug
body: <p>I have a fairly long script that ran fine until last night (no changes made, that I know of).</p>
  
  <p>The query that needs to be run is this:</p>
  
  <pre><code>$query = "INSERT INTO results SET 
      compId = ".$comp_id.",
      eventId = ".$eventId.",
      eventSession = '".$eventsession."',
      driverId = ".$driverid.",
      teamId = ".$teamid.",
      position = ".$this-&gt;data['drivers']['position'][$i].",
      raceTime = '".$racetime."',
      laps = ".$this-&gt;data['drivers']['laps'][$i].",
      pitstops = ".$this-&gt;data['drivers']['pitstops'][$i].",
      engine = ".$engineId.",
      tyres = '4',
      sector1 = '".$sector1."',
      sector2 = '".$sector2."',
      sector3 = '".$sector3."',
      fastestLap = '".$this-&gt;data['drivers']['bestTime'][$i]."',
      grid = ".$grid;
  
          // If debug is true then just var_dump but don't execute query
          if ($this-&gt;debug) {
              var_dump($query); echo '&lt;br&gt;';
          }
          else { // debug is false here, then not debug and run mysql query as usual
              mysql_query($query);
          }
  </code></pre>
  
  <p>I ran a debug/var_dump script that shows that all data is read correctly, and shows that the script correctly runs for each record in the dataset:</p>
  
  <pre><code>string(341) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 408, teamId = 142, position = 1, raceTime = '', laps = 9, pitstops = 0, engine = 57, tyres = '4', sector1 = '34.8867', sector2 = '35.8517', sector3 = '35.7191', fastestLap = '106.4575', grid = 0" 
  string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1910, teamId = 316, position = 2, raceTime = '', laps = 9, pitstops = 0, engine = 57, tyres = '4', sector1 = '35.0853', sector2 = '35.6669', sector3 = '35.7169', fastestLap = '106.4692', grid = 0" 
  string(343) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1934, teamId = 201, position = 3, raceTime = '', laps = 11, pitstops = 0, engine = 51, tyres = '4', sector1 = '35.1301', sector2 = '35.7618', sector3 = '35.7473', fastestLap = '106.6393', grid = 0" 
  string(343) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1263, teamId = 286, position = 4, raceTime = '', laps = 10, pitstops = 0, engine = 53, tyres = '4', sector1 = '35.0674', sector2 = '35.7036', sector3 = '35.9128', fastestLap = '106.6838', grid = 0" 
  string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1255, teamId = 286, position = 5, raceTime = '', laps = 8, pitstops = 3, engine = 53, tyres = '4', sector1 = '35.1663', sector2 = '35.9540', sector3 = '35.6342', fastestLap = '106.7544', grid = 0" 
  string(340) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1190, teamId = 8, position = 6, raceTime = '', laps = 3, pitstops = 0, engine = 51, tyres = '4', sector1 = '35.1284', sector2 = '35.8531', sector3 = '35.8777', fastestLap = '106.8593', grid = 0" 
  string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1092, teamId = 253, position = 7, raceTime = '', laps = 8, pitstops = 1, engine = 55, tyres = '4', sector1 = '35.2606', sector2 = '35.9344', sector3 = '35.7010', fastestLap = '106.8960', grid = 0" 
  string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1393, teamId = 154, position = 8, raceTime = '', laps = 9, pitstops = 0, engine = 58, tyres = '4', sector1 = '35.1497', sector2 = '35.8700', sector3 = '36.0847', fastestLap = '107.1044', grid = 0" 
  string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 2048, teamId = 251, position = 9, raceTime = '', laps = 9, pitstops = 1, engine = 55, tyres = '4', sector1 = '35.2091', sector2 = '36.1934', sector3 = '35.8702', fastestLap = '107.2728', grid = 0" 
  string(343) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 621, teamId = 308, position = 10, raceTime = '', laps = 12, pitstops = 0, engine = 51, tyres = '4', sector1 = '35.2224', sector2 = '36.0565', sector3 = '36.3339', fastestLap = '107.6127', grid = 0" 
  string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 447, teamId = 232, position = 11, raceTime = '', laps = 9, pitstops = 0, engine = 57, tyres = '4', sector1 = '35.1237', sector2 = '36.2297', sector3 = '36.2870', fastestLap = '107.6404', grid = 0" 
  </code></pre>
  
  <p>However, when not in debug, this only runs for the first record in the set. No matter what dataset I feed into.</p>
  
  <p>I am at a loss at explaining what is going wrong or if there is anything that stops the script after the first instance (which is entered correctly).</p>
  
  <p>Note: the original code is not mine and I no longer have access to the original developer as he left our group, so I am just basing myself of the current information. As mentioned earlier, this script has worked correctly until last night and no changes have been made to other parts of the code that may affect it.</p>
  
  <p>Thank you for any help!</p>
  
  <p>EDIT: here is the whole loop, as requested in the comments:</p>
  
  <pre><code>private function queryDb()
  {
  
      $year=gmdate("Y");
  
      // What $this-&gt;data holds at this point is an array with all drivers, positions, times, etc
      // using ['driver'][0] =&gt; Jim Parisis that correspond to ['position'][0] =&gt; 1 and the ['bestTime'][0] =&gt; 95.9577 and so on
  
      // Let's pick the event Id from $this-&gt;data
      $eventId = $this-&gt;data['event'];
      $eventsession = $this-&gt;data['evsess'];
  
      // instantiate the finances class
      // So we can use later any of its method like $finances-&gt;makeSomething($argument)
      $finances = new Finances($eventId, $this-&gt;debug);
  
      // Let's get the competition holding that event
      $query = "SELECT competition FROM ".$this-&gt;dbName.".calendar WHERE id='$eventId'";
      $result = mysql_query($query);
      // $comp_id = mysql_fetch_row($result); // What you have there is an array array(1) { [0]=&gt; string(1) "1" } 
      // That's true that competition is 1 but having in array you have to access it like $comp_id[0]
      while ($row = mysql_fetch_row($result))
          $comp_id = (int)$row[0]; // Better build it as simple integer to save memory
      mysql_free_result($result);
  
      // echo '&lt;pre&gt;'; print_r($this-&gt;data); echo '&lt;/pre&gt;';
      $i = 0; // Let's build some integer to play with because we have keys starting at 0 in the $this-&gt;data array
      foreach ($this-&gt;data['drivers']['name'] as $key =&gt; $value) {
  
          $driverFullName = $value; // now the foreach goes for each element of the 'drivers' key storing it in the $value variable
  
          // Gets the users id
          $query = "SELECT id FROM ".$this-&gt;dbName.".users WHERE CONCAT(firstName,' ',lastName) ='".$driverFullName."'";
          $result = mysql_query($query);
          while ($row = mysql_fetch_row($result))
              $driverid = $row[0];
          mysql_free_result($result);
  
          //var_dump($driverid);
  
          // Gets the partA from contracts
          $teamid = NULL;
  
          if ($comp_id==10) { $teamid= 0; }
          else { 
  
          $query = "SELECT partA FROM ".$this-&gt;dbName.".contracts WHERE partB ='".$driverid."' AND competition ='".$comp_id."' AND curState ='A'";
          $result = mysql_query($query);
          if (mysql_num_rows($result) != 0) {
              while ($row = mysql_fetch_row($result))
              $teamid = $row[0];
          }
          else { $teamid=0; echo "Warning: driver $driverFullName has no contract."; }
          mysql_free_result($result); 
          }
  
          //var_dump($teamid);
  
          // Gets the engine
          // We can wrap this and the next queries in just one by using LEFT JOIN but that's another story
          $enginemodel = NULL;
          $engineId = NULL;
  
          /*$query = "SELECT te.engine as engineId, e.engine as engineModel, en.id as engineId2 FROM DCO_team_engines te LEFT JOIN DCO_engines e ON te.engine=e.id LEFT JOIN engines en ON en.brand=e.engine WHERE te.team ='".$teamid."' AND te.comp='".$comp_id."' AND te.year ='".$year."'";
          $result = mysql_query($query);
          while ($row = mysql_fetch_row($result))
              $engineid = $row[2];
          mysql_free_result($result);*/
  
          //Substitute engine 
          $query = "SELECT engine FROM ".$this-&gt;dbName.".DCO_team_engines WHERE team ='".$teamid."' AND comp ='".$comp_id."' AND year ='".$year."'";
          $result = mysql_query($query);
          if (mysql_num_rows($result) != 0) {
              while ($row = mysql_fetch_row($result))
              $engineId = $row[0];
          }
          mysql_free_result($result); 
  
          $query = "SELECT vehchoice FROM ".$this-&gt;dbName.".competitions WHERE id ='".$comp_id."'";
          $result = mysql_query($query);
          while ($row = mysql_fetch_row($result))
              $vehchoice = $row[0];
          mysql_free_result($result); 
  
          if ($vehchoice==1) { 
              $query = "SELECT classes FROM ".$this-&gt;dbName.".licenses WHERE competition ='".$comp_id."' AND applicantId ='".$teamid."' AND concept ='2' AND validDate='".$year."'";
              $result = mysql_query($query);
              while ($row = mysql_fetch_row($result))
                  $engineId = $row[0];
              mysql_free_result($result); 
          }
  
          //var_dump($engineId); echo '&lt;br&gt;';
  
  
              // $this-&gt;data['sectors']['s1'][] = $sector1;
              // $this-&gt;data['sectors']['s2'][] = $sector2;
              // $this-&gt;data['sectors']['s3'][] = $sector3;
  
              $sector1 = $this-&gt;data['sectors']['s1'][$i];
              $sector2 = $this-&gt;data['sectors']['s2'][$i];
              $sector3 = $this-&gt;data['sectors']['s3'][$i];
              $laps = $this-&gt;data['drivers']['laps'][$i];
              $position = $this-&gt;data['drivers']['position'][$i];
              $finishstatus = $this-&gt;data['drivers']['finishStatus'][$i];
              $untimed = $this-&gt;data['drivers']['untimed'][$i];
              $carnumber = $this-&gt;data['drivers']['carnumber'][$i];
  
              if ($finishstatus=='Finished Normally') { $racetime = $this-&gt;data['drivers']['finishTime'][$i]; }
              if ($finishstatus=='DNF') { $racetime = "DNF"; }
  
              $grid = 0; // grid is always empty until the following condition applies
              if ($eventsession=='R' || $eventsession=='R1' || $eventsession=='R2' || $eventsession=='R3') { 
                  $laprank = $this-&gt;data['drivers']['fastestlaprank'][$i]; 
                  $grid = $this-&gt;data['drivers']['gridpos'][$i]; 
              }               
  
          if ($engineId == null)
              $engineId = 'DEFAULT';
  
          if ($comp_id=='12') { $carchosen = $this-&gt;data['drivers']['team'][$i]; 
  
              $query = "SELECT id FROM ".$this-&gt;dbName.".available_vehicles WHERE label ='".$carchosen."'";
              $result = mysql_query($query);
              while ($row = mysql_fetch_row($result))
                  $engineId = $row[0];
              mysql_free_result($result); 
  
          }   
  
          $query = "INSERT INTO results SET 
              compId = ".$comp_id.",
              eventId = ".$eventId.",
              eventSession = '".$eventsession."',
              driverId = ".$driverid.",
              teamId = ".$teamid.",
              position = ".$this-&gt;data['drivers']['position'][$i].",
              raceTime = '".$racetime."',
              laps = ".$this-&gt;data['drivers']['laps'][$i].",
              pitstops = ".$this-&gt;data['drivers']['pitstops'][$i].",
              engine = ".$engineId.",
              tyres = '4',
              sector1 = '".$sector1."',
              sector2 = '".$sector2."',
              sector3 = '".$sector3."',
              fastestLap = '".$this-&gt;data['drivers']['bestTime'][$i]."',
              grid = ".$grid;
  
          // If debug is true then just var_dump but don't execute query
          if ($this-&gt;debug) {
              var_dump($query); echo '&lt;br&gt;';
          }
          else { // debug is false here, then not debug and run mysql query as usual
              mysql_query($query);
          }
  
  
          // Ok, if you need to call fines method within the loop
          // call it here, for example to insert rows depending on the driver values like laps, pitstops, etc.
          // Notice we parse the whole array
  
          $query = "SELECT careerLadder, usesManagement, vehchoice, monoEngine FROM ".$this-&gt;dbName.".competitions WHERE id='$comp_id'";
          $result = mysql_query($query);
          while ($row = mysql_fetch_row($result)) {
              $careerLadder = (int)$row[0]; // Better build it as simple integer to save memory
              $usesManagement = (int)$row[1];
              $vehchoice = (int)$row[2];
              $monoEngine = (int)$row[3]; 
          }
          mysql_free_result($result);
  
  
          if ($comp_id!=10)
          {
              if (($eventsession!='T')&amp;&amp;($eventsession!='P')&amp;&amp;($eventsession!='Q'))
              {
                  $finances-&gt;standings($comp_id,$position, $driverid, $year, $teamid, $eventsession, $laps);
                  $finances-&gt;standingsarrange($comp_id);      
              }
          }       
  
          if ($usesManagement==1)
          {
              $finances-&gt;ladderfinances($teamid, $comp_id, $eventsession, $position, $laprank,$laps);
          }
  
          if ($comp_id==1) 
          { 
              if ($eventsession=='T') 
              {
                  $finances-&gt;rdpoints($teamid,$comp_id,$laps,$untimed);
                  $finances-&gt;erdpoints($teamid,$comp_id,$laps,$untimed);
              } 
  
              if (($eventsession!='T')&amp;&amp;($eventsession!='P'))
              {
                  $finances-&gt;enginewear($driverid,$teamid,$laps,$carnumber);
              }
          }
  
          $i++; // ok let's go to the next element if any, if foreach notices it's the last then it stops :)
      }
  
      $finances-&gt;standingstiebreak($comp_id);
  
      //if ($dom-&gt;getElementsByTagName('Qualify')-&gt;item(0))
      //  $this-&gt;data['session']      = $dom-&gt;getElementsByTagName('Qualify')-&gt;item(0)-&gt;nodeName;
  
      //if ($dom-&gt;getElementsByTagName('Practice1')-&gt;item(0))
      //  $this-&gt;data['session']      = $dom-&gt;getElementsByTagName('P')-&gt;item(0)-&gt;nodeName;
  
      if ($eventsession=='T') { $session = "Test"; }
      if ($eventsession=='P') { $session = "Practice"; }
      if ($eventsession=='Q') { $session = "Qualifying"; }
      if ($eventsession=="R") { $session = "Race"; }
      if ($eventsession=='R1') { $session = "Race 1"; }
      if ($eventsession=='R2') { $session = "Race 2"; }
      if ($eventsession=='R3') { $session = "Race 3"; }
  
      echo "&lt;h2&gt;$session results entered successfully&lt;/h2&gt;";
  </code></pre>