noushid p
noushid p

Reputation: 1483

Value IS NOT NULL in codeigniter

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);

Upvotes: 30

Views: 119862

Answers (4)

Janu Dewangga
Janu Dewangga

Reputation: 11

Try this:

$this->db->where('columnName !=', null);

Upvotes: -3

Nileshsinh Rathod
Nileshsinh Rathod

Reputation: 968

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();

Upvotes: 2

Deniz B.
Deniz B.

Reputation: 2562

Try this:

$this -> db -> get_where('donors', array('field !=' => NULL));

Upvotes: 14

Ari Djemana
Ari Djemana

Reputation: 1249

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');

Upvotes: 81

Related Questions