Home >Backend Development >PHP Tutorial >获取Codeigniter数据库查询的记录里的对象的字段值,该怎么处理

获取Codeigniter数据库查询的记录里的对象的字段值,该怎么处理

WBOY
WBOYOriginal
2016-06-13 09:58:51824browse

获取Codeigniter数据库查询的记录里的对象的字段值
这是codeigniter里的一段程序
models层
function checkUser()
{
  $username = $this->input->post('username');
  $password = $this->input->post('password');
  $this->db->from('user');
  $this->db->where('username',$username);
  $this->db->where('password',$password);
  $query=$this->db->get();
  return $query->result();
}
controller层:

public function User_Login()
{
  $this->load->model('User','',TRUE);
  $error['info']="用户名或者密码错误!";
  $data['query']=$this->User->checkUser();
  $info=$this->User->checkUser();
  //打印出这里获得的用户信息
  print_r($info);
  if(!empty($data['query']))
  {
  $this->load->view('User/User_Index');
  }
  else
  {
  $this->load->view('index',$error);
  }
 }

这里有个知识点有点纠结!
注释出打印出来的信息为:
Array ( [0] => stdClass Object ( [id] => 1 [createdate] => 2011-03-29 14:32:18 [flag] => 1 [password] => 123 [username] => user1 [email] => ) )


现在我如何让获得这个数组的第一个对象里的id???


------解决方案--------------------

PHP code
// modelfunction checkUser(){    //省略其他代码    //我的个人理解,这里只会返回一个对象,那么不如直接就返回一个内容    $res_data = $query->result_array();    // or $res_data = $query->result(); // 使用对象的话    if ((count($res_data) == 1))     {        $res_data = reset($res_data);    }    return $res_data;}// controllerpublic function User_Login(){    $this->load->model('User','',TRUE);    $error['info']="用户名或者密码错误!";    $data['query']=$this->User->checkUser();    $info=$this->User->checkUser();    //打印出这里获得的用户信息    print_r($info);    // 对象的话 $info->id    if(!empty($info['id']))    {        $this->load->view('User/User_Index');    }    else    {        $this->load->view('index',$error);    }}<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn