【问题标题】:row() return null for specific fieldrow() 为特定字段返回 null
【发布时间】:2016-03-22 22:32:19
【问题描述】:

我正在尝试将记录中的特定字段保存到会话中,以用于我的用户角色。这里的问题是除了 nama 之外我不能选择任何其他领域。

控制器/验证登录

public function index(){
    $this->load->model('verify_login_model');
    $username = $this->input->post('username');
    $password = $this->input->post('password');
    $result = $this->verify_login_model->verify($username, $password);
    if($result == $username) {



        $name = $this->verify_login_model->getinfo($username);
        $this->session->set_userdata('logged_in', TRUE);
        $this->session->set_userdata('username', $username);
        $this->session->set_userdata('name', $name);

        $this->load->view('home_view');






    } else {
        redirect('login');
    }

模型/verify_login_model

function verify($username, $password){
    $this->db->select('username', 'password');
    $this->db->from('tb_user');
    $this->db->where('username', $username);
    $this->db->where('password', MD5($password));
    $this->db->limit(1);

    $query = $this->db->get();
    if($query->num_rows()==1) {
        return $username;
    } else {
        return false;
    }
}

function getinfo($username) {
    $this->db->select('nama', 'username');
    $this->db->from('tb_userInfo');
    $this->db->where('username', $username);
    $this->db->limit(1);

    $query = $this->db->get();
    if($query->num_rows()==1) {

        $result = $query->row();

        return $result->nama;



    } else {
        return false;
    }
}

查看

<?php echo $this->session->userdata['name']?>

var_dump($result) : object(stdClass)#22 (1) { ["nama"]=> string(4) "test" }

如果我更改返回 $result->nama;到 $result-> 用户名;我得到错误:未定义的属性:stdClass::$username 即使我确定 200% 表中有用户名字段,并直接尝试查询。

【问题讨论】:

    标签: database codeigniter session model-view-controller stdclass


    【解决方案1】:

    你的select语句有错误,一定是

    $this->db->select('nama,username');
    

    您将每一列分开,这是不正确的,所有列都放在一个字符串中,这就是为什么它告诉您未定义的原因,因为您发送的唯一列是 nama 并且您将用户名作为第二个参数发送选择。

    这是 CodeIgniter 2.2.0

    active record 链接

    【讨论】:

      猜你喜欢
      • 2015-12-16
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      相关资源
      最近更新 更多