Try this:
<table>
<?php
$sql1 = "SELECT * FROM phpvms_pilots";
$pilots = DB::get_results($sql1);
$sql2 = "SELECT * FROM phpvms_airports";
$airps = DB::get_results($sql2);
foreach($airps as $airp)
{
if($airp->hub > 0)
{
$h = $airp->icao;
$p = "SELECT * FROM phpvms_pilots WHERE hub = '$h'";
$pi = DB::get_results($p);
?>
<thead>
<tr><td align="center" colspan="6" ><font size="4"><?php echo $airp->icao;?> (<?php echo $airp->name;?>)</font></td></tr>
<tr><th style="text-align: center;" bgcolor="#00405e" colspan="6"> </th></tr>
<tr>
<td>Pilot ID</td>
<td>Pilot Location</td>
<td>Pilot Name</td>
<td>Pilot Rank</td>
<td>Pilot Total Flights</td>
<td>Pilot Total Hours</td>
</tr>
</thead>
<?php
foreach($pi as $p)
{
if(!$pi)
{
?>
<tr><td colspan="6">No Pilots At This HUB</td></tr>
<?php
}
else
{
$rank = "SELECT * FROM phpvms_ranks WHERE rank = '$p->rank'";
$ran = DB::get_row($rank);
?>
<tr>
<td><a href="<?php echo url('/profile/view/'.$p->pilotid);?>"><?php echo $p->code.$p->pilotid;?></a></td>
<td><img src="<?php echo Countries::getCountryImage($p->location);?>" alt="<?php echo Countries::getCountryName($p->location);?>" ></td>
<td><?php echo $p->firstname.' '.$p->lastname?></td>
<td><img src="<?php echo $ran->rankimage?>" alt="<?php echo $p->rank;?>" ></td>
<td><?php echo $p->totalflights;?></td>
<td align="center"><?php echo Util::AddTime($p->totalhours, $p->transferhours); ?></td>
</tr>
<?php
}
}
}
}
?>
</table