Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with a SQL Query

Tags:

sql

php

sqlite

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?

like image 579
Skizit Avatar asked Jun 28 '10 10:06

Skizit


2 Answers

Perhaps...

 $q=mysql_query("SELECT * FROM `tweets` 
                       WHERE `lat` > $latlow AND
                             `lat` < $latupp AND 
                             `lng` >  $lnglow AND
                             `lng` < $lngupp;") or die(mysql_error());
like image 170
Brian Hooper Avatar answered Oct 04 '22 21:10

Brian Hooper


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

like image 32
das_weezul Avatar answered Oct 04 '22 21:10

das_weezul