0

我正在从数据库中进行选择,并且数据成功返回。我只是不确定如何访问数组的元素,例如用户名或名称等。这是输出:

Array ( [0] => stdClass Object ( [id] => 9 [fullname] => خالد الغاÙ...دي [membership] => free [username] = > kkkkk [密码] => 16d7a4fca7442dda3ad93c9a726597e4 [电子邮件] => someone@gmail.com [关于] => [城市] => 利雅得 [profilepic] => /home2/sdds/public_html/uploads/Red_velvet_cupcakes_with_roses.jpgRed_velvet_cupcakes_with_roses.jpg [mobile ] => [电话] => [地址] => [画廊链接] => ) )

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

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

那么在这里做什么呢?提前致谢

4

2 回答 2

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

于 2013-04-04T18:20:44.233 回答
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  

 } 
于 2013-04-05T00:04:35.910 回答