调用 CodeIgniter 的having() 方法时不应用值引用

发布于 2024-10-25 20:07:23 字数 423 浏览 4 评论 0原文

我对 Code Igniter 有条款有疑问。

我需要使用活动记录生成以下 SQL:

SELECT *
FROM A
GROUP BY A.X
HAVING A.Y = 'test'

但使用以下代码:

$this->db->select('*');
$this->db->from('A');
$this->db->group_by('A.X');
$this->db->having('A.Y','frontend');

生成:

SELECT *
FROM A
GROUP BY A.X
HAVING A.Y = test

似乎不可能转义字符串值...或者是吗?

I have a problem with Code Igniter having clause.

I need to produce the following SQL with active record:

SELECT *
FROM A
GROUP BY A.X
HAVING A.Y = 'test'

But using the following code:

$this->db->select('*');
$this->db->from('A');
$this->db->group_by('A.X');
$this->db->having('A.Y','frontend');

Produces:

SELECT *
FROM A
GROUP BY A.X
HAVING A.Y = test

And it seems impossible to escape the string value... Or is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌生 2024-11-01 20:07:23

以如此笨拙的方式编写having子句:

$this->db->having('A.Y = "frontend"', null, false);

Write the having clause in a such clumsy way:

$this->db->having('A.Y = "frontend"', null, false);
谜泪 2024-11-01 20:07:23

我不知道您使用的是哪个版本的CodeIgniter,但问题已不再存在。

return $this->db
    ->group_by('X')
    ->having('Y', 'frontend')
    ->get('A')
    ->result();

将构建一个与此类似的渲染 SQL 字符串(带有适当的引用):

SELECT *
FROM `A`
GROUP BY `X`
HAVING `Y` = 'frontend'

having() 方法调用受保护的 _wh() 方法,该方法是 where() 系列方法也使用。转义/引用过程都始终在那里进行。

I don't know what version of CodeIgniter you were using, but the problem no longer exists.

return $this->db
    ->group_by('X')
    ->having('Y', 'frontend')
    ->get('A')
    ->result();

Will build a rendered SQL string resembling this (with appropriate quoting):

SELECT *
FROM `A`
GROUP BY `X`
HAVING `Y` = 'frontend'

The having() method calls the protected _wh() method which the where() family of methods use as well. The escaping/quoting processes are all consistently baked-in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文