【问题标题】:CodeIngiter "WHERE AND OR" to Active RecordCodeIgniter "WHERE AND OR" 到 Activerecord
【发布时间】:2015-03-08 09:32:05
【问题描述】:

我想像这样进行 CodeIgniter Active Records 查询。

SELECT * FROM user WHERE user_name = 'email' 
AND (user_password='123' OR second_password = '456')

我已经尝试了一些代码,但无法正常工作。我正在使用 CI 2.2.0

【问题讨论】:

    标签: php mysql codeigniter activerecord


    【解决方案1】:

    根据 CI 文档,您可以为 where 子句 https://ellislab.com/codeigniter/user-guide/database/active_record.html 编写自定义字符串

    $this->db->from('user');
    $where = "user_name='email' AND (user_password='123' OR second_password = '456')";
    $this->db->where($where);
    $query = $this->db->get();
    

    【讨论】:

      【解决方案2】:

      简写版:

      $where = "name='Joe' AND status='boss' OR status='active'";
      
      $this->db->where('username', $name)->where($where)->get('myTableName');
      

      $this->db->where() 已经充当SELECT FROM * 除非您只想选择某些列。

      $this->db->select(); 注意:如果从表中选择所有 (*) 你不需要使用这个功能

      【讨论】:

        【解决方案3】:

        我有一个简单的解决方案,谢谢大家。

        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('user_name', 'email');
        $this->db->where('user_password', '123');
        $this->db->or_where('user_name', 'email');
        $this->db->where('second_password', '456');
        $query = $this->db->get();
        

        【讨论】:

          猜你喜欢
          • 2012-11-06
          • 1970-01-01
          • 2013-03-29
          • 1970-01-01
          • 2012-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多