SQL sytax error - on what?

3 replies
Hey Warriors,

I'm hoping we have some sql wizzes in here who can help me with this one. I've been boggled for hours trying to find a solution but it looks like there isn't much out there to help define my specific issue. Anyways - I am using geolocation to find coordinates via iphones and then use those coordinates to call from a database of nearby locations.

I had a simple method planned to set the accuracy but I cant figure out why im getting held up in the syntax. Anyways - here would be the code in question:

PHP Code:

$ulat = $_GET['glat'];
$ulong = $_GET['glong'];
$latpos = 0;
$longpos = 0;

// 

if($ulat < 0){
    $latpos = -1;
    $ulat = ($ulat*-1);
}else{
    $latpos = 1;
}

if($ulong < 0){
    $longpos = -1;
    $ulong = ($ulong*-1);
}else{
    $longpos = 1;
}

$latrangetop = $ulat + 0.01;
$latrangebottom = $ulat - 0.01;
$longrangetop = $ulong + 0.01;
$longrangebottom = $ulong - 0.01;

?>

<table>
<?php

// HERE IS THE SQL STRING THAT I GET AN ERROR ON
$lstring "SELECT * FROM locations WHERE latpos = ".$latpos." AND longpos = ".$longpos." AND lat > ".$latrangebottom." AND lat < ".$latrangetop." AND long > ".$longrangebottom.""
//END OF ERROR STRING

$lq mysql_query($lstring) or die(mysql_error());
while(
$la mysql_fetch_array($lq)){
    echo 
"<tr><td>".$la['name']."</td></tr>";
    
}
?>
</table>
The error doesnt occur unless I start checking against the long top/bottom ranges. It works perfectly with only latitude ranges but as soon as I start putting in the longitude it gives me a syntax error.

The exact error I get is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'long > 22.33333' at line 1
Any ideas? I've tried single quotes around them, between clauses and several different attempts. I could be missing something basic as I'm no programming genius - more of "duct-tape programmer" learning what I need to get by.
#error #sql #sytax
  • Change your field name and code from "long" to something else (like "longitude").

    Code:
    // HERE IS THE SQL STRING THAT I GET AN ERROR ON
     = "SELECT * FROM locations WHERE latpos = ".." AND longpos = ".." AND lat > ".." AND lat < ".." AND longitude > "..""; 
    //END OF ERROR STRING
    {{ DiscussionBoard.errors[2296971].message }}
    • Profile picture of the author Krisism
      Thank you Chris,

      That did solve it - I'm guessing long was some SQL clause? Anyways -thank you soo much for the help haha, I knew I just needed another keen person to take a look - all the research in the world wasn't helping because I didn't know what I was missing

      Props!

      Cheers,
      Kris
      {{ DiscussionBoard.errors[2297081].message }}
  • You're welcome. Yes, "long" is a reserved word, so it was causing the error.
    {{ DiscussionBoard.errors[2297135].message }}

Trending Topics