我的一个模型中有以下 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
我不知道为什么我的 table names
当我打电话时没有显示$this->db->get('names');
应该产生 SELECT * FROM names
,它只是没有返回错误吗?此声明有什么问题?我应该如何纠正我的 Active Record 调用?
最佳答案
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();
关于php - Codeigniter Active Record SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17126382/