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.
$cast_list = mysqli_query($dblink, $sql);
foreach ($cast_list as $role)
{
echo "<tr><td width='50%'>".$role['appeared_as']."</td>";
}
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.
$cast_list = mysqli_query($dblink, $sql);
$y = mysqli_num_rows($cast_list);
for ($x = 0; $x <$y; $x++)
{
$role = mysqli_fetch_assoc($cast_list);
echo "<tr><td width='50%'>".$role['appeared_as']."</td>";
}
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 var_dump($role)
for the first code block reveals that it is NULL
.
I can just adapt to this if need be, but maybe there's a logical reason why foreach
isn't working properly for me?