I'm trying to find the 10 entries that are closest to the user with the following search:
$data_query = mysqli_query($db, "SELECT *
111.045 * DEGREES(ACOS(COS(RADIANS(latpoint))
* COS(RADIANS(latitude))
* COS(RADIANS(longpoint) - RADIANS(longitude))
+ SIN(RADIANS(latpoint))
* SIN(RADIANS(latitude)))) AS distance_in_km
FROM merchants
JOIN (
SELECT 33.889676 AS latpoint, 151.193024 AS longpoint
) AS p ON 1=1
ORDER BY distance_in_km
LIMIT 15");
However I'm getting the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '111.045 * DEGREES(ACOS(COS(RADIANS(latpoint))
* COS(RADIANS(la' at line 2
I tried changing SELECT *
to SELECT latitude, longitude
(with `` but Stack Overflow keeps messing up syntax) but it's not doing much.
I'm new to PHP
and MySQL
so I'm feeling pretty out of depth as to what could be going wrong here.
Cheers :)