codeigniter返回对象

时间:2013-04-04 18:11:41

标签: codeigniter codeigniter-2

我从数据库和数据中成功选择回来。我只是不确定如何访问数组的元素,如用户名或名称等。这是输出:

  

数组([0] => stdClass对象([id] => 9 [fullname] =>خآد   Ø§Ù“غاÙ...دي[membership] =>免费[用户名] => KKKKK   [密码] => 16d7a4fca7442dda3ad93c9a726597e4 [email] =>   someone@gmail.com [about] => [city] => riyadh [profilepic] =>   /home2/sdds/public_html/uploads/Red_velvet_cupcakes_with_roses.jpgRed_velvet_cupcakes_with_roses.jpg   [mobile] => [电话] => [地址] => [gallery_link] => ))

这是返回上述内容的模型中的代码:

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();
}

那么该怎么办?提前谢谢

2 个答案:

答案 0 :(得分:1)

请检查一下:

//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;

参考:https://www.codeigniter.com/user_guide/database/results.html

答案 1 :(得分:0)

打桩 - 也要确定并确认你的模型方法中是否有任何东西

 // 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; } 
在你的控制器中你会立即知道是否有任何回复 并且您可以显示适当的视图。

  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  

 }