phpmysqlcodeigniteractiverecordwhere-clause

How to build a WHERE clause with OR between conditions using Codeigniter active record


I'm using the latest version of CodeIgniter and trying to write a simple query using the active record class, but can't quite figure out how to do an OR statement. Here's the code:

function get_active_trades()
{
    $query = $this->db->get_where('trades', array('trade_status !=' => 2));
    
    return $query->result();
}

Ideally, it should be if trade_status != 2 or user_trade_status != 2. However, I can't seem to find a way to make this happen. I've found the following on the help page, but can't seem to get it to work either:

$this->db->or_where();

Any suggestions on how this should be done with activerecord?


Solution

  • Like this:

    function get_active_trades() {
         $this->db->where('trade_status <>', 2);
         $this->db->or_where('user_trade_status <>', 2);
         return $this->db->get('trades')->result();
    }