CodeIgniter error 1064: asking for latitude and longitude
I have this request working 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 am trying to execute in CodeIgniter using 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 ... the user is about '*, (6371 * acos (cos (radians (21.161908)) * cos (radian (latitude))
Anyone have an idea with this error?
Note . I figured out the previous request before this.
+3
source to share
2 answers
Why don't you try 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");
0
source to share
You should use $this->db->select()
instead $this->select()
.
I had the same request like
$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 works fine.
0
source to share