1

I have the following mysql query in my PHP file:

$shopID   = mysql_real_escape_string($_GET['shop_id']);
$latitudeCenter = mysql_real_escape_string($_GET['lat_center']);
$longtitudeCenter = mysql_real_escape_string($_GET['lng_center']);

$lat_min = $latitudeCenter - 0.045;
$lat_max = $latitudeCenter + 0.045;
$long_min = $longtitudeCenter - (0.045 / cos($latitudeCenter*M_PI/180);
$long_max = $longtitudeCenter + (0.045 / cos($latitudeCenter*M_PI/180);

$sql = "SELECT * FROM shops WHERE shop_id = '$shopID' AND lat >= '$lat_min' AND lat <= '$lat_max' AND lng >= '$long_min' AND lng <= '$long_max'";

For some reason the query is not running successfully. Is the above query valid? Thanks

EDIT:

There is something wrong with the $long_min and $long_max calculations as when they are commented out, it works ok.

Here is the code I tried to conver to PHP:

lat_min = lat_center - 0.045;
lat_max = lat_center + 0.045;
long_min = long_center - (0.045 / Math.cos(lat_center*Math.PI/180);
long_max = long_center + (0.045 / Math.cos(lat_center*Math.PI/180);

What is wrong with my PHP?

6
  • What is the datatype of column lat? Commented Jul 20, 2012 at 13:47
  • Adding the error to your question would help out a lot. echo mysql_error(); after you executed mysql_query($sql);. Commented Jul 20, 2012 at 13:48
  • 1
    Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Jul 20, 2012 at 13:49
  • mysql_error() still returns a blank white page Commented Jul 20, 2012 at 13:51
  • Do you actually have data which can match your query? Commented Jul 20, 2012 at 13:53

1 Answer 1

2

Use the MySQl's BETWEEN and NEVER use quotes for comparing numbers. So the query becomes:

$sql = "SELECT *
  FROM shops
  WHERE shop_id = $shopID
    AND lat BETWEEN $lat_min AND $lat_max
    AND lng BETWEEN $long_min AND $long_max";

Where, I have considered that shop_id column is auto-incremental number.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks this seems correct, shop_id is actually VARCHAR, however i'm still getting a blank white page. I'm thinking there is something wrong with the maths calculations
@user1417302 Here is the code I tried to conver to PHP:. Convert the code from where? and Why

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.