Skip to content

Bug in count method with where and joins in null #1142

@FuriosoJack

Description

@FuriosoJack

Information

  • Version of Medoo: 2.2.0
  • Version of PHP: 7.4
  • Type of Database: MySQL
  • System: Linux

Describe the Problem
When using the count() method with the following signature:

count($tableName, null, null, $wheres);

The $where parameter is incorrectly replaced by the $join value due to the internal logic of Medoo.
As a result, the filters ($wheres) are ignored and an incorrect total count is returned.

The issue comes from the following part of the code:

if ($isJoin) {
    $tableQuery .= ' ' . $this->buildJoin($tableAlias ?? $table, $join, $map);
} else {
    if (is_null($columns)) {
        if (!is_null($where) || (is_array($join) && isset($columnFn))) {
            $where = $join;
            $columns = null;
        } else {
            $where = null;
            $columns = $join;
        }
    } else {
        $where = $columns;
        $columns = $join;
    }
}

As a consequence, $where becomes null because it gets overwritten by $join.

Code Snippet

$wheres = ['status' => 'active'];
$total = $database->count('users', null, null, $wheres);

var_dump($total); 
// Expected: WHERE status = 'active' applied
// Actual: WHERE is null, returning the total of all records

Expected Behavior
The count() method should respect the filters passed in $wheres and return the correct count based on those conditions.

Actual Behavior
The count() method ignores the filters and returns the total without applying the WHERE clause, because the $where variable is being overwritten by $join.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions