Php Codeigniter活动记录SQL语法错误
我的一个型号中有以下活动记录模式:Php Codeigniter活动记录SQL语法错误,php,mysql,codeigniter,activerecord,Php,Mysql,Codeigniter,Activerecord,我的一个型号中有以下活动记录模式: $this->db->get('names'); $this->db->like('name', $name); $this->db->where('ratio >=', 0.75); $this->db->order_by('ratio', 'desc'); $query = $this->db->get(); 这给了我一个语法错误: You have an error in your S
$this->db->get('names');
$this->db->like('name', $name);
$this->db->where('ratio >=', 0.75);
$this->db->order_by('ratio', 'desc');
$query = $this->db->get();
这给了我一个语法错误:
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 'WHERE `ratio` >= 0.75 AND `name` LIKE '%JORDAN%' ORDER BY `ratio' at line 2
返回的完整语句是:
SELECT * WHERE `ratio` >= 0.75 AND `name` LIKE '%JORDAN%' ORDER BY `ratio` desc
我不知道为什么在调用
$this->db->get('names')时,我的表names
没有显示出来
应该生成SELECT*FROM names
,它只是没有返回错误吗?此语句有什么问题?我应该如何更正我的活动记录呼叫 get
需要在末尾执行。如果要在之前选择一个表,请使用$this->db->from('names')然后只需$this->db->get()代码>。因此,完整代码:
$this->db->like('name', $name);
$this->db->where('ratio >=', 0.75);
$this->db->order_by('ratio', 'desc');
$query = $this->db->get('names');
或链接版本:
$query = $this->db->like('name', $name)
->where('ratio >=', 0.75)
->order_by('ratio', 'desc')
->get('names');
使用来自的:
$query = $this->db->from('names')
->like('name', $name)
->where('ratio >=', 0.75)
->order_by('ratio', 'desc')
->get();
谢谢链接版本对我来说更有意义。不客气!:)是的,它产生了更整洁的代码,但这仍然是一个偏好的问题,所以我向您展示了两种方法。