phpcodeignitermethodsactiverecordquery-builder

What is the purpose of the CodeIgniter's get() active record method?


I am using CodeIgniter for my application and I am a bit confused. I wrote some queries like this:

public function checkemail($email) {
    $this->db->select('email')->from('user')->where('email', $email);
}

But in the CodeIgniter active record manual, they talk about $this->db->get().

Should I add it after the $this->db->select() query?

My function doesn't produce any errors.

When should I use get()?


Solution

  • Yes, you'll need to run get() after the other methods. select(), from() and where() add their respective statements to the query, and get() actually runs the query and returns the result as an object.

    In this case, you could just add it on to the end of the chain.

    public function checkemail($email) {
        $this->db
            ->select('email')
            ->from('user')
            ->where('email', $email)
            ->get();
    }
    

    If you want to work with the result afterwards, make sure that you are assigning it to a variable.

    $user = $this->db
        ->select('email')
        ->from('user')
        ->where('email', $email)
        ->get();