php - Codeigniter Active Record SQL 语法错误

标签 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

我不知道为什么我的 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/

相关文章:

mysql - 如何使用 join in magento 获取集合

php - AuthUserFile 没有在配置错误中指定?

php - 无法找出这个正则表达式的解决方案

javascript - 如果用户在移动设备或低宽度上打开页面,则禁用 animate.css

PHP 和 JavaScript 错误

php - MySQL 查询 : generate a report of the top votes for the each food type

php - 我也想在 php/mysql 中导入 excel 文件和一些图像。有人有解决办法吗?

Codeigniter 不创建任何日志文件

javascript - 如何在页面底部自动显示和滚动隐藏的div?

javascript - 如何在php中获取服务器时间而不是本地时间