简体   繁体   English

codeigniter中的活动记录会自动在where子句值周围添加引号

[英]active record in codeigniter automatically adds quotes around where clause values

I've tried reading other posts on stackoverflow and also checked the active record documentation for ci, but i can't seem to find the answer to my question 我已经尝试阅读stackoverflow上的其他帖子,并检查了ci的活动记录文档,但我似乎无法找到我的问题的答案

I have the following logic in my model: 我的模型中有以下逻辑:

    $query = $this->db->get_where('categories', array('parent_id' => $category_id));

the sql this generates as per the last_query() method is: 根据last_query()方法生成的sql是:

SELECT * FROM ( categories ) WHERE parent_id = '8' SELECT * FROM( categories )WHERE parent_id ='8'

I need to remove the quotes around the number 8. How would I do that? 我需要删除数字8周围的引号。我该怎么做?

I've tried using the select statement and passing false as the second parm. 我已经尝试使用select语句并将false作为第二个parm传递。 So for example: 例如:

    $this->db->select('*', false);
    $this->db->from('categories');
    $this->db->where('parent_id=',$category_id);

But that didn't really change much. 但这并没有太大变化。 Any suggestions? 有什么建议么? Thank you 谢谢

By default, CodeIgniter tries to predict the data type in your comparison, and use the appropriate SQL syntax accordingly. 默认情况下,CodeIgniter会尝试预测比较中的数据类型,并相应地使用相应的SQL语法。 If your query is using single quotes, it might indicate that $category_id is being treated as a string rather than an integer. 如果您的查询使用单引号,则可能表示$ category_id被视为字符串而不是整数。 What happens if you try: 如果你尝试会发生什么:

$this->db->select('*');
$this->db->from('categories');
$this->db->where('parent_id', (int) $category_id);

Alternatively, you can construct your own WHERE statement manually: 或者,您可以手动构造自己的WHERE语句:

$this->db->where('parent_id = ' . (int) $category_id);

For MIN and MAX query I used null and false keyword to remove the quotes. 对于MIN和MAX查询,我使用null和false关键字来删除引号。

$this->db->where("$value > min_column",null,false);
$this->db->where("$value < max_column",null,false);

The idea of the methods is to auto escape to protect against SQL injections, if for some reason you don't want to you can send a raw query like this : 这些方法的想法是自动转义以防止SQL注入,如果由于某种原因你不想要你可以发送这样的原始查询:

$q = "select * from categories where parent_id = $category_id";
$this->db->query($q)->result();

Which i find much easier. 我觉得更容易。 However i think you can send an extra false paremeter to disable it, something like : 但是我认为你可以发送一个额外的false参数来禁用它,例如:

  $query = $this->db->get_where('categories', array('parent_id' => $category_id),false);

FYI, if you want to send raw queries and escape them(for more complex queries) you can use : 仅供参考,如果您想发送原始查询并将其转义(对于更复杂的查询),您可以使用:

$category_id = $this->db->escape($category_id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM