I am using a simple for loop to print field names of a table and it didn't work but the same code when I wrote using foreach loop it worked perfectly fine. I am a bit confused as to what might have caused it. Please help me find where I went wrong with the coding.
Code using foreach-loop:
$data=mysqli_query($con,"select * from `address` order by $id $sort");
echo "<tr>";
$field=mysqli_fetch_fields($data);
foreach ($field as $val) {
echo "<th><a href='sort_table_data_asc_desc.php?fn=$val->name &ord=$sort'>$val->name</a></th>";
}
echo "</tr>";
Output: as requested
Code using for-loop:
$data=mysqli_query($con,"select * from `address` order by $id $sort");
$col=mysqli_num_fields($data);
echo "<tr>";
$field=mysqli_fetch_fields($data);
for ($i=0;$i<$col;$i++) {
echo "<th><a href='sort_table_data_asc_desc.php?fn=$field->name &ord=$sort'>$field->name</a></th>";
}
echo "</tr>";
Output: as requested

