$value) { // We prefix the column with the table alias 'T' to avoid ambiguity in JOINs. $order[] = "T.`$key` " . $db->real_escape_string($value); } $sql .= " ORDER BY " . implode(", ", $order); } if (!$count && $limit) { $sql .= " LIMIT " . (int)$limit; } if (!$count && $offset) { $sql .= " OFFSET " . (int)$offset; } $result = $db->query($sql); if ($count) { return $result->fetch_assoc()['count']; } $data = []; while ($row = $result->fetch_assoc()) { $data[] = new self($row); } return $data; } }