title: php foreach not working as expected in one situation
body: <p>I like foreach. I've used it many times, successfully. While I don't necessarily understand the exact specifics of how or why it works, it has, until now, never stopped me from using it with the desired result. So here is my scenario...</p>
  
  <p>I have a bit of php on the backend of a form that allows users to upload certain types of files to be saved into a MySQL database. I know some of you will say saving a file inside a DB is not good, but lets just put that aside for now as there is very much a 50/50 split opinion on this and no real valid reason why it is bad, at all. The upload functionality works perfectly. I want the a display table to appear of the list of relevant files the user has placed into the DB. Here the weird foreach trouble starts. This is my code:</p>
  
  <pre><code>try {
      $stmt = $conn-&gt;prepare("SELECT * FROM $database.appFiles WHERE appID = :appid AND uaID = :uaid AND applicationKey = :applicationkey");
      $stmt-&gt;bindParam(':appid', $appID, PDO::PARAM_INT, 11);
      $stmt-&gt;bindParam(':uaid', $uaID, PDO::PARAM_INT, 11);
      $stmt-&gt;bindParam(':applicationkey', $applicationKey, PDO::PARAM_STR, 8);
      $stmt-&gt;execute();
      $fRes = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  } catch(PDOException $e) { catchMySQLerror($e-&gt;getMessage()); }
  if(count($fRes &gt; "0")) {
      //var_dump($fRes);
      foreach ($fRes as $value) {
          echo "  &lt;/tr&gt;\n";
          echo "  &lt;tr style=\"background: ".cycleColours()." border-bottom: 1px dotted #c0c0c0;\"&gt;\n";
          echo "   &lt;td style=\"padding: 5px;\"&gt;".$fRes[0][fileName]."&lt;/td&gt;\n";
          echo "   &lt;td style=\"padding: 5px;\"&gt;".$fRes[0][fileMime]."&lt;/td&gt;\n";
          echo "   &lt;td style=\"padding: 5px;\"&gt;".$fRes[0][fileSize]."&lt;/td&gt;\n";
          echo "   &lt;td style=\"padding: 5px;\"&gt;".uts2date($fRes[0][dateUploaded])."&lt;/td&gt;\n";
          echo "   &lt;td style=\"padding: 5px;\"&gt;Del&lt;/td&gt;\n";
      }
  
  } else {
      echo "  &lt;/tr&gt;\n";
      echo "  &lt;tr style=\"border-bottom: 1px dotted #c0c0c0;\"&gt;\n";
      echo "   &lt;td class=\"center\" style=\"padding: 5px;\" colspan=\"5\"&gt;&lt;i&gt;No files have been uploaded for this application yet&lt;/i&gt;&lt;/td&gt;\n";
  }
  </code></pre>
  
  <p>And when I have zero results in the DB I don't get any output (my "else" is not working), when I have one file uploaded I successfully get the output for that file as expected. When I have two files uploaded I get two rows each echoing out the details of the first file - repetition. No mention of the second file appears. This pattern goes on for the more files I have uploaded successfully. If 20 files have been uploaded, I get 20 rows all nice and tidy, telling me the same info about the first file uploaded, 20 times in a row.</p>
  
  <p>I have tried modifying my</p>
  
  <pre><code>    foreach ($fRes as $value) {
  </code></pre>
  
  <p>syntax and used:</p>
  
  <pre><code>    foreach ($fRes as $value =&gt; $key) {
  </code></pre>
  
  <p>But I get the same result. Seems to make no difference at all. The data extracted from the DB is an array (you can see my commented out var_dump($fRes); that I used to check it). Interestingly when I change the $fRes[0][fileName] to $fRes[1][fileName] I get the name of the second file, etc. This would seem to show that for some reason each file is in a parallel separate array? Why? And how do I make this work correctly as expected?</p>
  
  <p>Thanks</p>