2

i am getting values from datbase using get_where clause for my login like this

Controller:

public function  submit_login(){
            if($this->input->post('submit')){
                $email= $this->input->post('email');
                $password= $this->input->post('password');
                $this->load->model('user_model');
                $result= $this->user_model->login($email, $password);

              print_r($result);
            }
        }

Model

public function login($email, $password){
        $query=$this->db->get_where('user',array('email'=>$email,'password'=>$password));
             if($query->num_rows()>0){
                return $query->result();
            }
        return false ; 
    }

it returns followint array

Array ( [0] => stdClass Object ( [id] => 2 [name] => Sikander [email] => [email protected] [password] => password [mobile_number] => 1234 [role] => seller [points] => 0 ) )

Now i tried to loop this and assign id to sesion but foreach does not seem to work with this , dont know why pleas help

2 Answers 2

2

Application should work in matter model returns only one result. Which you have there. As you see you got an array with one element [0] that is an object. Following OOP approach you can reach those properties (values) by:

$result[0]->id;

Alternatively, as one row from database is expected, in model you can return:

return $query->row();

and check it by

$result->id

If array syntax is more convinient for you to iterate, you can use

$query->result_array()

or

$query->row_array() 

methods. There is plenty of useful examples in docs.

Sign up to request clarification or add additional context in comments.

Comments

0
public function login($email, $password){
    $query=$this->db->get_where('user',array('email'=>$email,'password'=>$password));
         if($query->num_rows()>0){
            return $query->result();
        }
    return false ; 
}

IN your code return $query->result(); give array with object so in

foreach ($users as $user) {
                  echo $user->email; // what ever your table fields          

                        }

If you want only single value form the query so you need to return $query->result(); replace with

return $query->row(); // this return as object  or 
return $query->row_array();// this return as single array  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.