mysql – 使用between子句的Codeigniter数据库错误
作者:互联网
我从codeigniter获取数据库语法错误并生成此sql查询.
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%'
我想我可以看到问题在于它们不应该在经度和纬度上加上它们.
生成此查询的代码是:
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;
}
解决方法:
CI 2.1.1 https://github.com/EllisLab/CodeIgniter/issues/1469中存在错误
在2.1.2出现之前,恢复到2.1.0是最佳解决方案
标签:mysql,codeigniter,codeigniter-2 来源: https://codeday.me/bug/20190902/1788129.html