I'm using CodeIgniter's Active Record class to query the MySQL database. I need to select the rows in a table where a field is not set to NULL:
$this->db->where('archived !=', 'NULL');
$q = $this->db->get('projects');
That only returns this query:
SELECT * FROM projects WHERE archived != 'NULL';
The archived
field is a DATE
field.
Is there a better way to solve this? I know I can just write the query myself, but I wan't to stick with the Active Record throughout my code.
Codeigniter generates an "IS NULL" query by just leaving the call with no parameters:
The generated query is:
CodeIgniter 3
Only:
The generated query is:
Inverse:
The generated query is:
And just to give you yet another option, you can use
NOT ISNULL(archived)
as your WHERE filter.Much better to use following For is not null
where('archived IS NOT NULL', null);
For is null
where('archived', null);
One way to check either column is null or not is
in php if column has data, it can be represent as True otherwise False To use multiple comparison in where command and to check if column data is not null do it like
here is the complete example how I am filter columns in where clause (Codeignitor). The last one show Not NULL Compression