'Currently Using CodeIgniter Framework i have an Error

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Login::$U_Model

Filename: controllers/Login.php

Line Number: 24

Backtrace:

File: C:\xampp\htdocs\Cmsproject\application\controllers\Login.php

Line: 24

Function: _error_handler

File: C:\xampp\htdocs\Cmsproject\index.php

Line: 315

Function: require_once

$data=$this->U_Model->loginf($loginf['username'], $loginf['password']);

An uncaught Exception was encountered

Type: Error

Message: Call to a member function loginf() on null

Filename: C:\xampp\htdocs\Cmsproject\application\controllers\Login.php

Line Number: 24

Backtrace:

File: C:\xampp\htdocs\Cmsproject\index.php

Line: 315

Function: require_once

Model File Is Here:

<?php
class U_Model extends CI_Model
{
    public function loginf($username,$pass){
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('username',$username);
        $this->db->where('password',$pass);
        if($query=$this->db->get()){
            return $query->row_array();
        }else{
            return false;
        }
    }
}
?>


Solution 1:[1]

Hope this will help you :

Your controller should be like this :

 <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {
    public function __construct(){
        parent::__construct();
        //$this->load->model('User');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->database();
        $this->load->model('U_Model');
    }
    public function index(){
        $this->load->view('header');
        $this->load->view('login');
        $this->load->view('footer');

    }

    /*added public before loginp*/

    public function loginp(){
        $username = $this->input->post('username');
        $password = md5($this->input->post('password'));
        /*md5 not recommended*/

        $data = $this->U_Model->loginf($username, $password);

        if( ! empty($data))
        {
            $this->session->set_userdata('sid',$data['id']);
            $this->session->set_userdata('sname',$data['name']);
            $this->session->set_userdata('username',$data['username']);
            redirect('login/home');
        }else{
            $this->session->set_flashdata('emsg','Please Enter Your Correct Name And Password');
            redirect('login/home');
        }
    }

    public function home(){
        $this->load->view('home');
    }
}?>

Change your model like this

<?php
class U_Model extends CI_Model
{
    public function loginf($username,$pass)
    {
        $this->db->where('username',$username);
        $this->db->where('password',$pass);
        $query = $this->db->get('users')
        if($query->num_rows() > 0){
            return $query->row_array();
        }else{
            return false;
        }
    }
}
?>

Solution 2:[2]

<?php
class U_Model extends CI_Model
{
    public function loginf($username,$pass)
    {
        $this->db->where('username',$username);
        $this->db->where('password',$pass);
        $query = $this->db->get('users')
        if($query->num_rows() > 0){
            return $query->row_array();
        }else{
            return false;
        }
    }
}
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 David Buck