title: Why is my foreach loop not working as expected?
body: <p>I'm pretty new to PHP development, but I'd like to think I'm picking it up at a rapid pace, but I hit a bit of a problem when I moved from XAMPP to a real host. I'm trying to do something like this.</p>
  
  <pre><code>$cast_list = mysqli_query($dblink, $sql);
  foreach ($cast_list as $role)
  {
      echo "&lt;tr&gt;&lt;td width='50%'&gt;".$role['appeared_as']."&lt;/td&gt;";
  }
  </code></pre>
  
  <p>It works on XAMPP which I have installed on my home PC, but it does not work on the hosting I've arranged to test with. The actual query is working perfectly, so that's not the problem. I can see the correct results in PHPMyAdmin, on XAMPP and when I change the code as follows. </p>
  
  <pre><code>$cast_list = mysqli_query($dblink, $sql);
  $y = mysqli_num_rows($cast_list);
  for ($x = 0; $x &lt;$y; $x++)
  {
      $role = mysqli_fetch_assoc($cast_list);
      echo "&lt;tr&gt;&lt;td width='50%'&gt;".$role['appeared_as']."&lt;/td&gt;";
  }
  </code></pre>
  
  <p>The second code block will produce the desired effect. The first code block will apparently iterate 5 times, but will not contain any meaningful data. In fact a <code>var_dump($role)</code> for the first code block reveals that it is <code>NULL</code>.</p>
  
  <p>I can just adapt to this if need be, but maybe there's a logical reason why <code>foreach</code> isn't working properly for me?</p>