Q: Elegant way of finding items within a latitude / longitude rectangle in an MySQL database? I have a MySQL database where each record contains, as well as many other things, a latitude and longitude. At the moment these are of type TEXT, though I would be happy to change this if necessary. One variable each for latitude and longitude, ie
latitude:  51.258215
longitude: -0.123456

So what I would like to know is the fastest, most elegant way of getting a list of records when a php script receives four POST variables... co-ords representing the top-left and bottom-right corners of a rectangle on a map..? Are there any ways to get the speed really optimised for this?

A: MySQL has some basic spatial extensions baked in (see spatial extensions). This allows you to store lat/long points as point records, for example:
INSERT INTO store_locations (id, name, location) 
VALUES (1, 'some name', POINT(51.258215, -0.123456));

You can then run spatial queries like so - for example with 'minimum bounding rectangle' operations: 
SELECT * FROM store_locations WHERE MBRWITHIN(location, MULTIPOINT($top $left, $bottom $right))

(where top, left, bottom and right are the relevant coordinates).

A: Separate out latitude and longitude into two different fields/columns. Index both of them and do a range query (BETWEEN clause or <= AND >= type clause) that should get you results pretty quickly.
