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->prepare(SELECT `ident`, `name`, `latitude_deg`, `longitude_deg`, `elevation_ft` FROM `airports` WHERE `ident` = :ident);$query_org->bindParam(:ident, $ident);$query_org->execute();$originDetails = $query_org->fetch(PDO::FETCH_ASSOC);$query_des = $db->prepare(SELECT `ident`, `type`, `name`, `latitude_deg`, `longitude_deg`, `elevation_ft` FROM `airports` WHERE `type` = :type);$query_des->bindParam(:type, $type);$query_des->execute();$num = 0;while ($destinDetails = $query_des->fetch(PDO::FETCH_ASSOC)) {    $distance = distance($originDetails['latitude_deg'], $originDetails['longitude_deg'], $destinDetails['latitude_deg'], $destinDetails['longitude_deg'], N);    if ($distance >= $data['dismin'] && $distance <= $data['dismax']) {        $a[$num] = array(            'ident'         => $destinDetails['ident'],            'type'          => $destinDetails['type'],            'name'          => $destinDetails['name'],            'elevation_ft'  => $destinDetails['elevation_ft'],            'distance'      => $distance        );        $num++;    }}$num = null;$num = 0;foreach ($a as $z) {    $i = $z['ident'];    $query_run = $db->prepare(SELECT `length_ft` FROM `runways` WHERE `airport_ident` = :ident);    $query_run->bindParam(:ident, $i);    $query_run->execute();    $result = $query_run->fetch(PDO::FETCH_ASSOC);    $length = $result['length_ft'];    if ($length >= $data['minrunlen'] && $length <= $data['maxrunlen']) {        $b[$num] = array(            'icao' => $z['ident'],            'name' => $z['name'],            'distance' => $z['distance']            );        $num++;    }}$count = count($b);$int = rand(0, $count);//Query for Flight_ID$queryc = $db->prepare(SELECT `fid` FROM `dispatches` ORDER BY `id` DESC);$queryc->execute();$flight_id = $queryc->fetch(PDO::FETCH_NUM);    if ($flight_id[0] !== 1000) {    $flight_id = (int)$flight_id[0];    $flight_id++;} else {    $flight_id = 1000;}$dispatch = array(    'airline'       => strtoupper($data['airline']),    'hub'           => strtoupper($data['hub']),    'pid'           => $data['pid'],    'pilot'         => $data['fname'].' '.$data['lname'],    'email'         => $data['email'],    'fid'           => $flight_id,    'airframe'      => $data['airframe'],    'datetime'      => date(Y-m-d H:i:s),    'connumb'       => md5(rand(0,9999999999)),    'originICAO'    => $originDetails['ident'],    'originName'    => utf8_encode($originDetails['name']),    'destinICAO'    => $b[$int]['icao'],    'destinName'    => utf8_encode($b[$int]['name']),    'distance'      => $b[$int]['distance'].nm);//Query for runway information$destinICAO = $dispatch['destinICAO'];$queryd = $db->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->bindParam(:ident, $destinICAO);$queryd->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.
Virtual Airline Dispatch Generator
php;mysql