3

I am getting a database syntax error from codeigniter and its generating this sql query.

SELECT `code`, `latitude`, `longitude` FROM (`postcodes`) WHERE `latitude` `BETWEEN 55`.`8616 AND 56`.`1508` AND `longitude` `BETWEEN '-3`.`9507' AND '-3`.`6131'` AND `code` LIKE 'FK%'

I think I can see the problem its adding '' where they should not be in the longitude and lqatitudes.

The code thats generating this query is:

 function get_zips_in_range($zip, $range, $sort=1, $include_base=true)
{
    //get base postcode details
    $details = $this->get_zip_point($zip);

    if( ! $details)
    {
        return false;
    }

    //get the first 2 letters of the postcode
    $str = substr($zip, 0, 2);

    //find max - min lat / long for radius and zero point and query
    //only zips in that range.
    $lat_range = $range/69.172;
    $lon_range = abs($range/(cos($details->latitude) * 69.172));
    $min_lat = number_format($details->latitude - $lat_range, "4", ".", "");
    $max_lat = number_format($details->latitude + $lat_range, "4", ".", "");
    $min_lon = number_format($details->longitude - $lon_range, "4", ".", "");
    $max_lon = number_format($details->longitude + $lon_range, "4", ".", "");

    //build the sql query
    $this->CI->db->select("code, latitude, longitude");

    if( !$include_base)
    {
        $this->CI->db->where("code <>", $zip);
    }

    $this->CI->db->where("latitude BETWEEN '$min_lat' AND '$max_lat'");
    $this->CI->db->where("longitude BETWEEN '$min_lon' AND '$max_lon'");
    $this->CI->db->like('code', $str, 'after');    
    $result = $this->CI->db->get("postcodes");

    if($result->num_rows() < 1)
    {
        $this->set_last_error("SQL error in get_zips_in_range");
        return false;
    }
2
  • it seems to be adding single quotes around the points in the lon/lats like so Commented Jun 14, 2012 at 12:29
  • See also the canonical: using BETWEEN in WHERE condition Commented Oct 31, 2023 at 6:01

4 Answers 4

3

There is a bug in CI 2.1.1 https://github.com/EllisLab/CodeIgniter/issues/1469

Reverted to 2.1.0 is the best solution until 2.1.2 comes out

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

Comments

1

CodeIgniter automatically adds backticks to make safer queries. If you want to disable this, you can add the parameter FALSE as follows:

$this->CI->db->where("latitude BETWEEN '$min_lat' AND '$max_lat'", FALSE);

Comments

1

Well, check out following thing,

$this->CI->db->where("latitude BETWEEN $min_lat AND $max_lat");
$this->CI->db->where("longitude BETWEEN $min_lon AND $max_lon");

apart from syntax, it will be rendered as where latitude BETWEEN $min_lat AND $max_lat AND longitude BETWEEN $min_lon AND $max_lon

Now you will ask what is the problem..

The problem is in query the way we are making it

It can be done as follows

$this->CI->db->where("(latitude BETWEEN $min_lat AND $max_lat)");
$this->CI->db->where("(longitude BETWEEN $min_lon AND $max_lon)");

So it will be rendered as where (latitude BETWEEN $min_lat AND $max_lat) AND (longitude BETWEEN $min_lon AND $max_lon) and hopefully this will solve the problem.

Even there is no need to quotes.

Comments

0
$this->CI->db->where("latitude BETWEEN '$min_lat' AND '$max_lat'");
$this->CI->db->where("longitude BETWEEN '$min_lon' AND '$max_lon'")

remove the simple '

$this->CI->db->where("latitude BETWEEN $min_lat AND $max_lat");
$this->CI->db->where("longitude BETWEEN $min_lon AND $max_lon")

8 Comments

Just tried it the error remains: 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 'BETWEEN 55.8616 AND 56.1508 AND longitude BETWEEN -3.9507 AND -3.61' at line 3 SELECT code, latitude, longitude` FROM (postcodes) WHERE latitude BETWEEN 55.8616 AND 56.1508 AND longitude BETWEEN -3.9507 AND -3.6131 AND code LIKE 'FK%'
can you print the complete query to see how it looks like?
SELECT code, latitude, longitude FROM (postcodes) WHERE latitude BETWEEN 55.8616 AND 56.1508 AND longitude BETWEEN -3.9507 AND -3.6131 AND code LIKE 'FK%'
your query looks ok, can you put the tables structure and some data in sqlfiddle.com to make test?
The query is ok as it works a direct sql query the problem is that ci is putting single quotes around the point in the numbers but its not showing that when I paste the query in here instead of 56.1508 the ci query is 56"."1508.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.