Question

I know there are many questions that deal with this error but I've done what they have asked to fix the issue I thought. Below is what I have done but I'm still getting the error. The goal of this script is to display all the zipcodes within a certain radius.

$zip = 94550; // "find nearby this zip code"
$radius = 15; // "search radius (miles)"
$maxresults = 10; // maximum number of results you'd like

$sql = "SELECT * FROM
    (SELECT o.zipcode, o.city, o.state,
        (3956 * (2 * ASIN(SQRT(
        POWER(SIN(((z.latitude-o.latitude)*0.017453293)/2),2) +
        COS(z.latitude*0.017453293) *
        COS(o.latitude*0.017453293) *
        POWER(SIN(((z.longitude-o.longitude)*0.017453293)/2),2)
        )))) AS distance
    FROM zipcoords z,
        zipcoords o,
        zipcoords a
    WHERE z.zipcode = ".$zip." AND z.zipcode = a.zipcode AND
        (3956 * (2 * ASIN(SQRT(
        POWER(SIN(((z.latitude-o.latitude)*0.017453293)/2),2) +
        COS(z.latitude*0.017453293) *
        COS(o.latitude*0.017453293) *
        POWER(SIN(((z.longitude-o.longitude)*0.017453293)/2),2)
        )))) <= ".$radius."
    ORDER BY distance)
    ORDER BY distance ASC LIMIT 0,".$maxresults;

$result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());   

while ($ziprow = mysql_fetch_array($result)) {
$zipcode = $ziprow['zipcode'];
echo "$zipcode<br>";
}

All my columns in the database are varchar. zipcode is the primary and I would make it INT but it doesn't allow there to be 0's at the beginning of the zipcodes. So I changed it to varchar and it allowed it. Thanks for the help!

Was it helpful?

Solution

$sql = "SELECT * FROM
    (SELECT o.zipcode, o.city, o.state,
        (3956 * (2 * ASIN(SQRT(
        POWER(SIN(((z.latitude-o.latitude)*0.017453293)/2),2) +
        COS(z.latitude*0.017453293) *
        COS(o.latitude*0.017453293) *
        POWER(SIN(((z.longitude-o.longitude)*0.017453293)/2),2)
        )))) AS distance
    FROM zipcoords z,
        zipcoords o,
        zipcoords a
    WHERE z.zipcode = ".$zip." AND z.zipcode = a.zipcode AND
        (3956 * (2 * ASIN(SQRT(
        POWER(SIN(((z.latitude-o.latitude)*0.017453293)/2),2) +
        COS(z.latitude*0.017453293) *
        COS(o.latitude*0.017453293) *
        POWER(SIN(((z.longitude-o.longitude)*0.017453293)/2),2)
        )))) <= ".$radius."
    ORDER BY distance) AS random_word_to_silence_that_error
    ORDER BY distance ASC LIMIT 0,".$maxresults;

I added random_word_to_silence_that_error because in your case I suppose it will serve no other purpose.

OTHER TIPS

An alias is mandatory when selecting FROM a subquery.

See: http://dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html

Just add an AS <alias name> at the end of the subquery you're selecting from:

...
ORDER BY distance) AS SOME_ALIAS
ORDER BY distance ASC LIMIT 0
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top