title: Creating a custom MySQL function?
body: <p>I've just started using MySQL with PHP and I'd like to know if it's possible to create a custom function. This code snippet should illustrate what I'm trying to do.</p>
  
  <pre><code>// a somewhat complicated formula not suitable to embed into the query
  function Distance($latA, $lonA, $latB, $lonB)
  {
    // earth's radius
    $radius = 3956;
  
    $latA = deg2rad($latA);
    $lonA = deg2rad($lonA);
    $latB = deg2rad($latB);
    $lonB = deg2rad($lonB);
  
    // calculate deltas
    $deltaLat = $latB - $latA;
    $deltaLon = $lonB - $lonA;
  
    // calculate Great Circle distance
    $result = pow(sin($deltaLatitude / 2.0), 2) + (cos($latA) * cos($latB) * pow(sin($deltaLon / 2.0), 2));
    $distance = $radius * 2 * atan2(sqrt($result), sqrt(1 - $result));
  
    return $distance;
  }
  
  // how can I call Distance() in my query?
  $query = "SELECT lat, lon FROM zipcodes WHERE Distance(lat, lon, 0, 0) &lt; 20";
  mysql_query($query);
  </code></pre>
  
  <p>Thanks in advance for any help!</p>