29

I am trying to create the following statement:

select * from donors where field is NOT NULL;

With codeigniter, my code looks like this:

$where = ['field' => NULL];
$this->db->get_where('table', $where);
codewitharefin
  • 1,398
  • 15
  • 24
noushid p
  • 1,453
  • 5
  • 16
  • 29
  • 2
    Possible duplicate of [Codeigniter Where clause](http://stackoverflow.com/questions/10109047/codeigniter-where-clause) – vhu Oct 12 '15 at 09:13

4 Answers4

79

when you see documentation You can use $this->db->where() with third parameter set to FALSE to not escape your query. Example:

$this->db->where('field is NOT NULL', NULL, FALSE);

Or you can use custom query string like this

$where = "field is  NOT NULL";
$this->db->where($where);

So your query builder will look like this:

$this->db->select('*');
$this->db->where('field is NOT NULL', NULL, FALSE);
$this->db->get('donors');

OR

$this->db->select('*');
$where = "field is  NOT NULL";
$this->db->where($where);
$this->db->get('donors');
Ari Djemana
  • 1,229
  • 12
  • 12
14

Try this:

$this -> db -> get_where('donors', array('field !=' => NULL));
Deniz B.
  • 2,532
  • 1
  • 18
  • 35
2

If you have some a complex query with multiple where and Having condition.

Please find below example:

$this->db->select(['id', 'email_address', 'abandon_at','datediff(now(),`abandon_at`) AS daysdiff ' ]); 
$this->db->having('daysdiff < 11');
$query = $this->db->get_where('forms', array('abandon_form' => 1,'abandon_at !=' => 'NULL') );   
return $query->result();
Nileshsinh Rathod
  • 948
  • 1
  • 17
  • 37
  • This worked for me. In my case I had to make sure 'NULL' was in quotes like in this example: `'abandon_at !=' => 'NULL'` – WTFranklin Feb 04 '21 at 20:13
-3

Try this:

$this->db->where('columnName !=', null);
  • Welcome to Stackoverflow. Please elaborate a bit to explain how your answer works and how it is different from accepted answer. – Nabil Farhan Aug 29 '19 at 05:57