我的__construct中有错误

I'm just new in codeigniter and I have this error

A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$data

Filename: drivers/Session_database_driver.php

Line Number: 171

Backtrace:

File: C:\Apache24\htdocs\iCareFinal\application\controllers\Welcome.php Line: 20 Function: __construct

File: C:\Apache24\htdocs\iCareFinal\index.php Line: 292 Function: require_once

Here is my controller welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {

public function __construct()
{
    parent::__construct();


    $this->load->model('login/m_login');

    $this->load->library(array('form_validation','session'));

    $this->load->database();

    $this->load->helper('url');

}


public function index()
{


    $session = $this->session->userdata('isLogin');

    if($session == FALSE)

    {

    redirect('welcome/login_form');


    }
    else

    {

    redirect('home');

    }


}



public function login_form()

{



    $this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean');

    $this->form_validation->set_rules('password', 'Password', 'required|md5|xss_clean');

    $this->form_validation->set_error_delimiters('<span class="error">', '</span>');

    if($this->form_validation->run()==FALSE)

    {

    $this->load->model('select');
    $user=$this->select->getid();
    $this->load->view('include/loginpage/header');
    $this->load->view('loginView/loginPage');
    $this->load->view('include/loginpage/footer');

    }else

    {

    $username = $this->input->post('username');

    $password = $this->input->post('password');


    $cek = $this->m_login->takeUser($username, $password);

    if($cek <> 0)

    {

    $this->session->set_userdata('isLogin', TRUE);

    $this->session->set_userdata('username',$username);




    // save data to a session
    $user_id = 199911019;

    $newdata = array(
            'user_id'  => $user_id

            );

    $this->session->set_userdata($newdata);


    redirect('home');

    }else

    {

    echo " <script>

    alert('Failed Login: Check your username and password! $password');

    history.go(-1);

    </script>";

    }

    }

}

public function logout()

{

    $this->session->sess_destroy();

    redirect('welcome/login_form');

}

}

Model m_login.php

<?php

if(!defined('BASEPATH')) exit('Hacking Attempt : Get Out of the system ..!');

class M_login extends CI_Model

{

public function __construct()

{

parent::__construct();

}

public function takeUser($username, $password)

{

$this->db->select('*');

$this->db->from('USERS');

$this->db->where('USERNAME', $username);

$this->db->where('PASSWORD', $password);


$query = $this->db->get();

return $query->num_rows();

}

public function userData($username)

{

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

$this->db->select('name');

$this->db->where('username', $username);

$query = $this->db->get('USERS');

return $query->row();

}

}

View loginPage.php

<body>
<div>
<div class="welcome">
     <h4 class="text-muted mt-0 font-alt" style="font-size: 20px; font-family: 'Times New Roman', Times, serif;" >Welcome to myADNU</h4>
</div>
<hr class="hr-width">

<form action="<?php echo site_url('Welcome/index'); ?>" method="post">
    <div id="bootstrap-tagsinput" class="form-group has-feedback">
<input type="text" class="form-control" placeholder="ID Number" name="username" />
        <i class="glyphicon glyphicon-user form-control-feedback"></i>
    </div>
    <br>
    <div id="bootstrap-tagsinput" class="form-group has-feedback">
        <input type="password" class="form-control" placeholder="Password" name="password" />
        <i class="glyphicon glyphicon-lock form-control-feedback"></i>
    </div>
    <br>
    <div id="bootstrap-tagsinput">
        <button type="submit" class="btn btn-primary btn-lg btn-block">SUBMIT</button>
    </div>

</form>
</div>

</div><!-- /.container -->

Here is the my session table name CI_SESSION DATA VARCHAR2(500 BYTE) ID VARCHAR2(200 BYTE) IP_ADDRESS VARCHAR2(20 BYTE) TIMESTAMP NUMBER(20, 0)

Please help. Thank you

I think the issue is your line:

$this->load->database() which appear after you load the models.

I suggest that you put it in the array $this->load->library() with the others. You do not need to load it separately.

Phillip

Remove this line from controller constructor

$this->load->database();

And add this line in model constructor. Your model will look like:

public function __construct(){
        parent::__construct();
        $this->load->database();
    }

Your controller constructor will look like

public function __construct(){
    parent::__construct();
    $this->load->model('login/m_login');
    $this->load->library(array('form_validation','session'));
    $this->load->helper('url');
}

Check this,

Controller welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('login/m_login');
}

public function index()
{
    $session = $this->session->userdata('isLogin');
    if($session == FALSE)
    {
        redirect('welcome/login_form');
    }
    else
    {
        redirect('home');
    }
}

public function login_form()
{
    $this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'required|md5|xss_clean');
    $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
    if($this->form_validation->run()==FALSE)
    {
        $this->load->model('select');
        $user=$this->select->getid();
        $this->load->view('include/loginpage/header');
        $this->load->view('loginView/loginPage');
        $this->load->view('include/loginpage/footer');
    }
    else
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $cek = $this->m_login->takeUser($username, $password);

        if($cek <> 0)
        {
            $this->session->set_userdata('isLogin', TRUE);
            $this->session->set_userdata('username',$username);
            // save data to a session
            $user_id = 199911019;
            $newdata = array(
                'user_id'  => $user_id
            );
            $this->session->set_userdata($newdata);
            redirect('home');
        }
        else
        {
            echo " <script>
            alert('Failed Login: Check your username and password! $password');
            history.go(-1);
            </script>";
        }
    }
}

public function logout()
{
    $this->session->sess_destroy();
    redirect('welcome/login_form');
}

}

Module m_login.php

<?php
class M_login extends CI_Model

public function takeUser($username, $password)
{
    $this->db->select('*');
    $this->db->from('USERS');
    $this->db->where('USERNAME', $username);
    $this->db->where('PASSWORD', $password);
    $query = $this->db->get();
    return $query->num_rows();
}

public function userData($username)
{
    $this->db->select('username');
    $this->db->select('name');
    $this->db->where('username', $username);
    $query = $this->db->get('USERS');
    return $query->row();
}

}

And add these line in autoload.php in config folder.

$autoload['libraries'] = array('database', 'session','form_validation',);

$autoload['helper'] = array('url');

It will solve your problem.