Achraf Almouloudi
Achraf Almouloudi

Reputation: 746

CodeIgniter model unable to return specific records

I am using CodeIgniter and I'm unable to get the where() selection method to work when using the results() method to fetch all the rows from a table.

Here's my model:

public function get_all_entries($id)
    {
        // Select row to be fetched
        $this->db->where('id', $id);
        $this->db->get('users');
        // Execute the find query with provided data
        $query = $this->db->get('users');
        // Return an object with all the data
        return $query->result();
    }

It is supposed to return all the rows that match the $id argument in the users table, but instead, it simply fetches all the records in the table, including those that doesn't match the provided $id argument.

What I'm doing wrong here? I've tried the row() method and although it works with the where() it only returns a single row so it doesn't suit my case.

Upvotes: 1

Views: 46

Answers (1)

nestedl00p
nestedl00p

Reputation: 490

The problem is that you are calling the get() method twice, the first one with the where, but is not assign to a variable; the second one is assigned to a variable, but since the where clause is already used by the other one it gets everything. delete the first get and you should be fine.

public function get_all_entries($id)
{
    // Select row to be fetched
    $this->db->where('id', $id);
    // Execute the find query with provided data
    $query = $this->db->get('users');
    // Return an object with all the data
    return $query->result();
}

Upvotes: 1

Related Questions