我的模型:
function version()
{
$this->load->database();
$this->db->select('location_id');
$this->db->from('geo_blocks');
$this->db->where("2057793231 BETWEEN `ip_start` AND `ip_end`");
echo $query=$this->db->get()->row();
}
我收到的错误:
A Database Error Occurred
Error Number: 1054
Unknown column ' ip_start <= 2057793231 AND ip_end >= 2057793231' in 'where clause'
SELECT `location_id` FROM (`geo_blocks`) WHERE ` ip_start <= 2057793231 AND ip_end >= 2057793231
Line Number: 93
但是查询在 PHPMyAdmin 中运行良好:
SELECT * FROM `geo_blocks` WHERE 2057793231 BETWEEN `ip_start` AND `ip_end`
我还尝试了不同的查询,它们在 PHPMyAdmin 中产生相同的输出,但在 CodeIgniter 中则不然:
$this->db->where("ip_start <= 2057793231 AND ip_end >= 2057793231");
我做错了什么?
最佳答案
您使用的 where() 函数是错误的。 where 函数的全部作用是让系统确定列在哪里,以及要转义什么值以防止 mysql 注入(inject)。 在这种情况下,第一个参数需要是列,第二个参数需要是值:
$this->db->where('ip_start <=', 2057793231);
$this->db->where('ip_end >=', 2057793231);
请注意,您可以多次调用 where() 函数。它只是在两个条件之间添加一个 AND。
关于CodeIgniter 中的 MySQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7514040/