如何在codeigniter中设置数据库中的会话userdata?

时间:2017-01-11 06:23:50

标签: php mysql codeigniter

我在codeigniter 3中登录会话有问题。我无法从数据库中获取数据以插入会话。我的代码怎么了?

我的控制器
    

<?php 
class M_login extends CI_Model{ 
function cek_login($table,$where){      
    return $this->db->get_where($table,$where);
    }   
}

我的模特

<div styleName={styles.treeNode}>some text </div>

3 个答案:

答案 0 :(得分:0)

 Try This 
if($cek > 0){
   $cek = $cek->row_array();
    $data_session = array(
        'username' => $cek['username'],
        'nama'     => $cek['nama']
        'status'   => "login"
        );
    $this->session->set_userdata($data_session);
    redirect(base_url("admin"));
}else{
    echo "Username dan password salah !";
}

答案 1 :(得分:0)

试试这个......

在codeigniter中return以数组格式获取第一个匹配的行。所以row_array()必须是这样的......

function aksi_login()

有关详情,请参阅此处.. https://www.codeigniter.com/userguide3/database/results.html

答案 2 :(得分:0)

1.m_login-&gt; cek_login
  此函数返回SQL QUERY OBJECT
2. SQL QUERY OBJECT - &gt; num_rows()函数返回INT($ cek)
3.so $ cek是INT。所以你不能像数组一样使用($ cek [&#39;用户名&#39;])

所以请尝试使用此代码

function aksi_login(){
    $username = $this->input->post('username');
    $password = $this->input->post('password');
    $where = array(
        'username' => $username,
       'password' => md5($password)
    );
   $cek = $this->m_login->cek_login("admin",$where);//$cek is a Query Object
   $rows = $cek->num_rows();//$rows is a INT
   if($rows > 0){
        $cek = $cek->row_array();//now u get an Array
        $data_session = array(
           'username' => $cek['username'],
           'nama'     => $cek['nama']
           'status'   => "login"
           );
        $this->session->set_userdata($data_session);
        redirect(base_url("admin"));
    }else{
        echo "Username dan password salah !";
    }
}