Virtual Airline Dispatch Generator This Code is old and I am trying to figure out how to improve it. I am considering running the distance function in MySQL, but it is a ***** to figure out for me due to my experience with arithmetic in MySQL.

$id = $_GET['id'];
    $data = $_SESSION[$id]; 
    $airline = $data['airline'];
    $hub = $data['hub'];    
    $ident = $data['originAirport'];
    $type = $data['type'];

//Query for Origin Airport Details
$query_org = $db-&gt;prepare("SELECT `ident`, `name`, `latitude_deg`, `longitude_deg`, `elevation_ft` FROM `airports` WHERE `ident` = :ident");
$query_org-&gt;bindParam(":ident", $ident);
$query_org-&gt;execute();
$originDetails = $query_org-&gt;fetch(PDO::FETCH_ASSOC);
$query_des = $db-&gt;prepare("SELECT `ident`, `type`, `name`, `latitude_deg`, `longitude_deg`, `elevation_ft` FROM `airports` WHERE `type` = :type");
$query_des-&gt;bindParam(":type", $type);
$query_des-&gt;execute();
$num = 0;
while ($destinDetails = $query_des-&gt;fetch(PDO::FETCH_ASSOC)) {
    $distance = distance($originDetails['latitude_deg'], $originDetails['longitude_deg'], $destinDetails['latitude_deg'], $destinDetails['longitude_deg'], "N");
    if ($distance &gt;= $data['dismin'] &amp;&amp; $distance &lt;= $data['dismax']) {
        $a[$num] = array(
            'ident'         =&gt; $destinDetails['ident'],
            'type'          =&gt; $destinDetails['type'],
            'name'          =&gt; $destinDetails['name'],
            'elevation_ft'  =&gt; $destinDetails['elevation_ft'],
            'distance'      =&gt; $distance
        );
        $num++;
    }
}

$num = null;
$num = 0;

foreach ($a as $z) {
    $i = $z['ident'];
    $query_run = $db-&gt;prepare("SELECT `length_ft` FROM `runways` WHERE `airport_ident` = :ident");
    $query_run-&gt;bindParam(":ident", $i);
    $query_run-&gt;execute();
    $result = $query_run-&gt;fetch(PDO::FETCH_ASSOC);
    $length = $result['length_ft'];
    if ($length &gt;= $data['minrunlen'] &amp;&amp; $length &lt;= $data['maxrunlen']) {
        $b[$num] = array(
            'icao' =&gt; $z['ident'],
            'name' =&gt; $z['name'],
            'distance' =&gt; $z['distance']
            );
        $num++;
    }

}


$count = count($b);



$int = rand(0, $count);

//Query for Flight_ID
$queryc = $db-&gt;prepare("SELECT `fid` FROM `dispatches` ORDER BY `id` DESC");
$queryc-&gt;execute();
$flight_id = $queryc-&gt;fetch(PDO::FETCH_NUM);    
if ($flight_id[0] !== 1000) {
    $flight_id = (int)$flight_id[0];
    $flight_id++;
} else {
    $flight_id = 1000;
}
$dispatch = array(
    'airline'       =&gt; strtoupper($data['airline']),
    'hub'           =&gt; strtoupper($data['hub']),
    'pid'           =&gt; $data['pid'],
    'pilot'         =&gt; $data['fname'].' '.$data['lname'],
    'email'         =&gt; $data['email'],
    'fid'           =&gt; $flight_id,
    'airframe'      =&gt; $data['airframe'],
    'datetime'      =&gt; date("Y-m-d H:i:s"),
    'connumb'       =&gt; md5(rand(0,9999999999)),
    'originICAO'    =&gt; $originDetails['ident'],
    'originName'    =&gt; utf8_encode($originDetails['name']),
    'destinICAO'    =&gt; $b[$int]['icao'],
    'destinName'    =&gt; utf8_encode($b[$int]['name']),
    'distance'      =&gt; $b[$int]['distance']."nm"
);
//Query for runway information
$destinICAO = $dispatch['destinICAO'];
$queryd = $db-&gt;prepare("SELECT `length_ft`, `width_ft`, `lighted`, `closed`, `le_ident`, `le_latitude_deg`, `le_longitude_deg`, `le_elevation_ft`, `le_heading_degT`, `le_displaced_threshold_ft`, `he_ident`, `he_latitude_deg`, `he_longitude_deg`, `he_elevation_ft`, `he_heading_degT`, `he_displaced_threshold_ft` FROM `runways` WHERE `airport_ident` = :ident");
$queryd-&gt;bindParam(":ident", $destinICAO);
$queryd-&gt;execute();


Any suggestions are helpful. I am not looking for anybody to rewrite the code. That is my job, but guidance is more what I am looking for.
Prepare once - execute multiple times

Prepared statements not only prevent SQL injection, but also enable you to only prepare a statement once, and then execute it multiple times with different values for increased performance:

$query_run = $db-&gt;prepare("SELECT `length_ft` FROM `runways` WHERE `airport_ident` = :ident");
$query_run-&gt;bindParam(":ident", $i);

foreach ($a as $z) {
    $i = $z['ident'];
    $query_run-&gt;execute();
    [...]
}


Naming


don't use short, non-expressive variable names. a, z, i (as non-loop variable), b, etc are not telling a reader much.
use either camelCase or snake_case, but don't mix them.
write out words. what's an org or a des? what is a destin? why is ident an originAirport? 
What's a num? (ok, a number, but what kind of number? Seems to be a counter, so counter would be more fitting. But what does it count?
int is also not a very good name. It seems to be a random index? Then randomIndex would be better.


Misc


variables which are only used in one place, and whos name doesn't really add anything can be removed. Eg $count = count($b); is not needed, just use this directly: $int = rand(0, count($b));
if you split your code up into methods, it will be easier to read. You might have methods such as getOriginAirport, meassureDistancesFrom, getFlightId, getRunwayInfo, etc.