Codeigniter Active Record SQL语法错误

时间:2013-06-15 17:58:22

标签: php mysql codeigniter activerecord

我的一个模型中有以下Active Record模式:

$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

我不知道为什么我的表names没有显示,因为我正在调用应生成$this->db->get('names');的{​​{1}},它是否只是在错误中返回?这句话有什么问题,我应该怎么做才能纠正我的Active Record电话?

1 个答案:

答案 0 :(得分:2)

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');

使用from

$query = $this->db->from('names')
->like('name', $name)
->where('ratio >=', 0.75)
->order_by('ratio', 'desc')
->get();