2

我有一个奇怪的问题一直困扰着我。我要做的就是在数据库表上运行查询,但由于某种原因,CodeIgniter 将撇号放入查询中,这随后会破坏页面。

我的代码如下所示:

$this->db->select("SUBSTRING(body,5)"); 
$this->db->order_by("date", "desc");
$this->data['query'] = $this->db->get_where('blog-entries', array('status' => 'P'), 3);

但我在此页面上收到错误消息:

Error Number: 1064

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 'FROM (`blog-entries`) WHERE `status` = 'P' ORDER BY `date` desc LIMIT 3' at line 2

查询实际上运行为:

SELECT SUBSTRING(body, `5)` FROM (`blog-entries`) WHERE `status` = 'P' ORDER BY `date` desc LIMIT 3

正如您所看到的,由于某种原因,在子字符串中的数字 5 周围添加了撇号。如果我删除子字符串,那么一切正常,如果我删除撇号并直接在我的数据库上运行查询,它也可以工作。

有没有人知道为什么会发生这种情况或有解决方案?

您的帮助将不胜感激。

非常感谢,

G。

4

1 回答 1

5

Use this:

$this->db->select("SUBSTRING(body,5)", FALSE);

As a default, Codeigniter tries to add back-ticks where it thinks is relevant. Sometimes it adds them where it shouldn't. Passing FALSE as the second parameter prevents it from doing this.

于 2012-04-28T11:32:44.010 回答