title: PHP inconsistently generates a 500 server error when looping through an array
body: <p>I was using the following code to retrieve HTML snippets from a database table and display them. It worked fine on my old web host, but after moving to a new web host I started getting (rather unhelpful) <code>500 Internal Server</code> errors. Both hosts use PHP 5.2.x.</p>
  
  <pre><code>$query = "SELECT id, html FROM $tableName ORDER BY id DESC LIMIT 0, 300";
  $result = mysql_query($query);
  while ($row = mysql_fetch_assoc($result)) {
      $myArray[] = $row;
  }
  
  foreach($myArray as $m) {
      echo $m['html'];
  }
  </code></pre>
  
  <p>By selectively commenting out code, I narrowed the problem down to the foreach loop. I eventually found that I could get the page to display (with no 500 error) if I chopped off some of the items (see <code>$offset</code> below). Sometimes I have to use an offset of 50, sometimes 100 or more.</p>
  
  <pre><code>$counter = 0;
  $offset = 100;
  $limit = count($myArray) - $offset;
  while ($counter &lt; $limit) {
      echo $myArray[$counter]['html'];
      ++$counter;
  }
  </code></pre>
  
  <p>This made me think there was something wrong with the individual HTML snippets. So I adjusted the offset one by one until I found the offending row (i.e., <code>$offset=23</code> worked, but <code>$offset=22</code> doesn't, therefore row #23 is the culprit). I looked at that row's HTML and it is perfectly fine. Not only that, but earlier in the day my script had even displayed that particular HTML snippet with no issues (this table periodically has new HTML inserted, and I'm just viewing the most recent 300 of them).</p>
  
  <p>I also tried adding some basic checks before echo-ing, but it had no effect:</p>
  
  <pre><code>while ($counter &lt; $limit) {
      if ($myArray[$counter]['html'] != false &amp;&amp; !empty($myArray[$counter]['html'])) {
          echo $myArray[$counter]['html'];
      }
      ++$counter;
  }
  </code></pre>
  
  <p>Any ideas why echo and/or the loop is failing? How can I see useful errors instead of a 500 server error? I have PHP display_errors turned on and I can see errors from other parts of the script when I intentionally force them (both on the page and in the error log file).</p>
  
  <p><strong>Update: Apache access log</strong></p>
  
  <p>Okay, I went to it first and manually set '$offset' to 200 (see the parameter <code>?o=1</code> in the URL) which I knew would let the page display properly. The result:</p>
  
  <pre><code>my.ip.add.ress - p [18/Jun/2010:13:27:36 -0400] "GET /test2/index.php HTTP/1.1" 200 602778 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)"
  my.ip.add.ress - p [18/Jun/2010:13:27:47 -0400] "GET /test2/index.php?o=200 HTTP/1.1" 200 418127 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)"
  </code></pre>
  
  <p>Then I forced <code>$offset</code> to 1 (which would generate the 500 error) and I got this:</p>
  
  <pre><code>my.ip.add.ress - - [18/Jun/2010:13:31:06 -0400] "GET /test2/index.php?o=1 HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)"
  my.ip.add.ress - p [18/Jun/2010:13:30:59 -0400] "GET /test2/index.php HTTP/1.1" 200 602731 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)"
  </code></pre>
  
  <p>It says <code>404</code>, however the page being displayed in the browser says two things: the <code>&lt;title&gt;</code> is 500 Internal Server error, and the page body repeats this but then also mentions the 404 because it couldn't find the 500 error HTML page (which I haven't set up yet).</p>