views:

92

answers:

3

Hey Guys,

I'm looking for a SQLite query which will return values that are great or less than 0.014763 for the lng and 0.008830 for the lat. I'm using the following code...

 $latupp = $_REQUEST['currlat'] + 0.008830; 
 $latlow = $_REQUEST['currlat'] - 0.008830;
 $lngupp = $_REQUEST['currlng'] + 0.014763;
 $lnglow = $_REQUEST['currlng'] - 0.014763;
 $q=mysql_query("SELECT * FROM `tweets` 
                       WHERE `lat` > $latlow AND
                             `lat` < $latupp OR 
                             `lng` >  $lnglow AND
                             `lng` < $lngupp;") or die(mysql_error());

But it's returning incorrect results e.g results greater or less than 0.1441209. I can't quite figure out why. does anyone have any suggestions?

+7  A: 

Perhaps...

 $q=mysql_query("SELECT * FROM `tweets` 
                       WHERE `lat` > $latlow AND
                             `lat` < $latupp AND 
                             `lng` >  $lnglow AND
                             `lng` < $lngupp;") or die(mysql_error());
Brian Hooper
This appears to of worked! thanks!!
Ulkmun
A: 

Try this.

$q=mysql_query("SELECT * FROM `tweets` 
                       WHERE `lat` >". $latlow. "AND
                             `lat` <". $latupp. "OR 
                             `lng` >".  $lnglow. "AND
                             `lng` <". $lngupp.";")
Judas Imam
Didn't work, thanks for the effort tho!
Ulkmun
Its maybe related about AND/OR keywords usage priority. You use them with brackets
Judas Imam
+4  A: 

That should work:

 $latupp = $_REQUEST['currlat'] + 0.008830;  
 $latlow = $_REQUEST['currlat'] - 0.008830; 
 $lngupp = $_REQUEST['currlng'] + 0.014763; 
 $lnglow = $_REQUEST['currlng'] - 0.014763; 
 $q=mysql_query("SELECT * FROM `tweets`  
                       WHERE `lat` BETWEEN $latlow AND $latupp   
                             AND
                             `lng` BETWEEN  $lnglow AND $lngupp;") or die(mysql_error()); 

Maybe you need parenthesis to separate the first and second part (Can't test it here).

You made a little mistake by connecting the lat and the long constraint with an "OR". Your command says:

Give me all records which have:

  • a lat value greater than $latlow AND lower than $latupp, NO MATTER what long value it has got

OR

  • a long value greater than $lnglow AND lower than $lngupp NO MATTER what lat value it has got

OR BOTH

das_weezul