I built a mobile app that needs to locate events going on around them based on their current latitude and longitude with a 10 mile radius. The user clicks on an event type and the API needs to search for all events within 10 miles of them. The way my database is set up each event belongs to a venue and that is where the latitude and longitude is stored. Here is the SQL that I have so far:
$sql = "SELECT events.*, (3959 * acos(cos(radians(78.3232)) * cos(radians($latitude)) * cos(radians($longitude) - radians(65.3234)) + sin(radians(78.3232)) * sin(radians($latitude)))) AS distance
FROM events
INNER JOIN venues ON events.venue_id = venues.id
INNER JOIN promoters ON events.promoter_id = promoters.id
WHERE type = $type AND end > NOW()
ORDER BY distance";
The events table stores information about an event (name, description, start, end, etc.) while the venues table stores information about a venue (name, address, latitude, longitude, etc.). How would I join these tables to make sure that I retrieve the events that I want based on the user's location?