I am trying to do a COUNT that has WHERE clauses in it with the code below:
function count_tasks($userId)
{
$this->db->count_all('tasks');
$this->db->where('tasksAssignedTo', $userId);
$query = $this->db->where('taskPriority_taskPriorityId !=', 6);
return $query;
}
However Codeigniter only seems to be running the following query:
SELECT COUNT(*) AS `numrows` FROM `tasks`
Why are the WHERE clause conditions not included/executed?
I guess it has something to do with returning $query
but I don't know for sure.
Move the count_all('tasks')
call to be after the where()
calls.
The count_all()
function stops the SQL building of the class.
function count_tasks($userId)
{
$this->db->where('tasksAssignedTo', $userId);
$this->db->where('taskPriority_taskPriorityId !=', 6);
$query = $this->db->count_all('tasks');
return $query;
}
The returned value $query
will be an integer-type value.
The executed query WILL NOT include the WHERE logic because count_all()
ignores all SQL segments compiled before its call.
SELECT COUNT(*) AS `numrows` FROM `tasks`