0

I am selecting from database and data successfully coming back. I am just not sure how to access element of the array such as username or name or etc. This is the output:

Array ( [0] => stdClass Object ( [id] => 9 [fullname] => خالد الغامدي [membership] => free [username] => kkkkk [password] => 16d7a4fca7442dda3ad93c9a726597e4 [email] => [email protected] [about] => [city] => riyadh [profilepic] => /home2/sdds/public_html/uploads/Red_velvet_cupcakes_with_roses.jpgRed_velvet_cupcakes_with_roses.jpg [mobile] => [telephone] => [address] => [gallery_link] => ) )

and this is the code in the model returning the above:

public function login()
{
    $this->db->select('*');
    $this->db->from('members');
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', md5($this->input->post('password')));
    $query = $this->db->get();
    return $query->result();
}

So what to do here? thanks in advance

3
  • foreach through the results yo. Commented Apr 4, 2013 at 18:13
  • gave me this: Message: Object of class stdClass could not be converted to string Commented Apr 4, 2013 at 18:15
  • $query->result() returns an object, not an array. Use $query->result_array() instead if you want an array. It's a common mistake :) Commented Apr 4, 2013 at 18:29

2 Answers 2

1

please check this :

//model function in your model    
public function login()
    {
        $this->db->select('*');
        $this->db->from('members');
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get();
        return $query->row();
    }
   // in controller  
    $logindata = $this->YOUR_MODEL_NAME->login();
// access like this

    echo $logindata->fullname;

Ref: https://www.codeigniter.com/user_guide/database/results.html

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

Comments

0

piling on -- also be sure and check if you actually got anything in your model method

 // check how many records returned with num_rows()
 if ($query->num_rows() == 1) {

       // since there is just one record, use row()
       // when 2 or more records, use result() 
       return $query->row();
    }
    else
    { return false; } 

that way in your controller you immediately know if anything came back and you can show the appropriate view.

  if(! $logindata = $this->login_model->login() ) { 
   // NO login data came back 
   //  show appropriate view

  else {
 // pass to data if you need it in the view 
  $data['logindata'] = $logindata ;
   // call and show the view  

 } 

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.