简体   繁体   中英

CodeIgniter Error 1064: Latitude and Longitude query

I have this query which works in phpMyAdmin

SELECT *, 
    ( 6371 * acos ( cos ( radians(21.161908) ) 
    * cos( radians( `latitude` ) ) 
    * cos( radians( `longitude` ) - radians(-86.85152790000001) ) 
    + sin ( radians(21.161908) ) 
    * sin( radians( `latitude` ) ) ) ) AS distance 
FROM _results 
HAVING distance < 30 
ORDER BY distance 
LIMIT 0 , 20

and I'm trying to execute in CodeIgniter with this:

$this->select("* , ( 6371 * acos ( cos ( radians(21.161908) ) * cos( radians(latitude) ) * cos( radians( longitude ) - radians(-86.85152790000001) ) + sin ( radians(21.161908) ) * sin( radians( latitude ) ) ) ) AS distance ");
$this->db->having('distance < 30');
$this->db->order_by('distance');
$this->db->limit(20,0);

But with this I get this error:

Error Number: 1064 You have an error in your SQL Syntax... to user near '*, (6371 * acos ( cos ( radians(21.161908) ) * cos( radians(latitude) )

Anyone have idea with this error?

Note : I realized a previous query before this.

You should use $this->db->select() instead of $this->select() .

I had a same type query like this

$this->db->select("*, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance");                         
$this->db->having('distance <= ' . $miles);                     
$this->db->order_by('distance');                    
$this->db->limit(20, 0);

which is working fine.

Why don't you try like this

$this->db->query("SELECT *, 
    ( 6371 * acos ( cos ( radians(21.161908) ) 
    * cos( radians( `latitude` ) ) 
    * cos( radians( `longitude` ) - radians(-86.85152790000001) ) 
    + sin ( radians(21.161908) ) 
    * sin( radians( `latitude` ) ) ) ) AS distance 
FROM _results 
HAVING distance < 30 
ORDER BY distance 
LIMIT 0 , 20");

Working perfect for me.

$this->db->select("SELECT *,ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $Lat ) ) + COS( RADIANS( `latitude` ) )* COS( RADIANS( $Lat )) * COS( RADIANS( `longitude` ) - RADIANS( $Long )) ) * 6380 AS `distance`");
            $this->db->from('_results');
            $this->db->where("ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `latitude` ) )* COS( RADIANS( $fLat )) * COS( RADIANS( `longitude` ) - RADIANS( $fLon )) ) * 6380 < 5");
            $this->db->orderby('distance');
            $this->db->limit(20,0);
            return $this->db->get()->result();
 function lat_long($lat,$long){
  
 $query = $this->db->query("SELECT *, ( 6371 * acos ( cos ( radians($lat) ) * cos( radians( `latitude` ) ) * cos( radians( `longitude` ) - radians($long) ) + sin ( radians($lat) ) * sin( radians( `latitude` ) ) ) ) AS distance FROM table HAVING distance < 30 ORDER BY distance LIMIT 0 , 20");     
 return $query->result();

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM