【问题标题】:How to construct an active record query in codeigniter?如何在codeigniter中构造一个活动记录查询?
【发布时间】:2012-10-10 18:38:39
【问题描述】:

我最近开始将我的 codeigniter 应用程序中的后端查询转换为活动记录模式。截至目前,我正在使用 codeigniter 2.1 版本。我最初在 mysql 中写了一个查询作为

SELECT * FROM table WHERE (field1 = $field1 OR field2 = $field2) AND field3 = $field3 AND field4 = 'text'

现在我想用下面给出的方式来写这个东西:

$this->db->select('*');
$this->db->from('table');
$this->db->where('field1',$field1);
$this->db->or_where('field2',$field1);
$this->db->where('field3',$field2);
$this->db->where('field4','text');

但不知何故,“or_where”功能无法正常工作。对此有任何解决方案,我会很高兴吗?

【问题讨论】:

标签: php codeigniter activerecord


【解决方案1】:

你在Active Record写自定义字符串

$where = "(field1 = " . $field1 . " OR field2 = " . $field2 . ") AND 
          field3 = ". $field3 . " AND field4 = 'text'";
$this->db->where($where);

Click here for source: Active Record (and browse for custom string)

【讨论】:

  • 谢谢兄弟...但我认为会有更安全的查询中不涉及任何纯 sql 编码。
猜你喜欢
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多