-1

I have a problem in a MYSQL query ran from a php script.

In my sql statement I need to filter all the users that are too far away so:

SELECT
...
ROUND(ACOS(SIN(RADIANS($Lat)) *
    SIN(RADIANS(s.Latitude)) + 
    COS(RADIANS($Lat)) *
    COS(RADIANS(s.Latitude)) * 
    COS(RADIANS(s.Longitude) - 
    RADIANS($Lon))) * $unitKm, 2) AS "Distance"
...
HAVING "Distance" <= $Dis

Now, there's a user that is 440.55 Km away but, if I pass 334 as "Dis", it's not filtered.

333 is ok, but when bigger than 333, the HAVING clause does not work.

I pass $Dis as a number and get it from php via a:

$Dis = filter_input(INPUT_GET, 'Dis');

How could I solve the problem ?

marco
  • 1,686
  • 1
  • 25
  • 33
  • 1
    You probably need to use `WHERE` instead of `HAVING`. See http://stackoverflow.com/a/2905312/1864610 –  Mar 12 '15 at 22:04
  • Thanks, tried but does not solve the problem. But I'll read the link, for sure I'll learn something. – marco Mar 12 '15 at 22:09
  • 1
    Presumably, there is an issue with your distance calculation or with the variables used to define it. The `having` clause is fine. – Gordon Linoff Mar 12 '15 at 22:10

1 Answers1

1

Not "Distance" that is a string; you need to use backtics `Distance` in the HAVING clause.

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Perfect, thanks. I'm using backtics in all the php files beside this one, always thinking "I'll replace them...". – marco Mar 13 '15 at 07:50