CodeIgniter Forums
Call to a member function userdata() on a non-object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Call to a member function userdata() on a non-object (/showthread.php?tid=41494)

Pages: 1 2


Call to a member function userdata() on a non-object - El Forum - 05-09-2011

[eluser]djkemical[/eluser]
Hi everybody
I have a code that give me this error
Code:
Call to a member function userdata() on a non-object in C:\wamp\www\ci\application\libraries\centinela.php on line 19
I have no idea why give me this error...
The code of centinela.php is
Code:
<?php
if(!defined('BASEPATH'))
    exit('No direct script access allowed');

class Centinela extends CI_Controller
{

    var $_user = "";
    var $_password = "";

    var $_auth = FALSE;

    function Centinela($auto = TRUE)
    {
        if($auto)
        {
            $CI =& get_instance();

            if($this->login($CI->session->userdata('user'), $CI->session->userdata('password')))
            {
                $this->_user = $CI->session->userdata('user');
                $this->_password = $CI->session->userdata('password');
            }
        }
    }
    function getUser(){return $this->_user;}
    function getPassword(){return $this->_password;}
    function login($user = "", $password = "")
    {
        if(empty($user)||empty($password))
            return FALSE;

        $CI =& get_instance();        

        $sql = "SELECT * FROM `usuarios` WHERE `user`=? AND `password`=?";
        $query = $CI->db->query($sql, array($user, $password));

        //login ok
        if($query->affected_rows()==1)
        {
            $row = $query->row();

            $CI->session->set_userdata('user', $user);
            $this->_user = $user;
            $CI->session->set_userdata('password', $password);
            $this->_password = $row->password;

            $this->_auth = TRUE;

            return TRUE;
        }
        else
        {
            $this->_auth = FALSE;
            $this->logout();

            return FALSE;
        }
    }
    function logout()
    {
        $CI =& get_instance();
        $CI->session->sess_destroy();
        $this->_auth = FALSE;
    }
}
?>
Thanks in advance


Call to a member function userdata() on a non-object - El Forum - 05-09-2011

[eluser]InsiteFX[/eluser]
For one you do not need to use the $CI-> Super Object in a Controller!

Just autoload the database and session libraries...

InsiteFX


Call to a member function userdata() on a non-object - El Forum - 05-11-2011

[eluser]djkemical[/eluser]
Thanks for your reply.
The centinela.php is not a controller, is a library


Call to a member function userdata() on a non-object - El Forum - 05-11-2011

[eluser]InsiteFX[/eluser]
Then why are you extending it as a CI_Controller?

InsiteFX


Call to a member function userdata() on a non-object - El Forum - 05-11-2011

[eluser]djkemical[/eluser]
Up's...
I fixed the code, but it gives me the same error.
Code:
<?php
if(!defined('BASEPATH'))
    exit('No direct script access allowed');

class Centinela
{

    var $_user = "";
    var $_password = "";

    var $_auth = FALSE;

    function Centinela($auto = TRUE)
    {
        if($auto)
        {
            $CI =& get_instance();
            $CI->load->database();
            if($this->login($CI->session->userdata('user'), $CI->session->userdata('password')))
            {
                $this->_user = $CI->session->userdata('user');
                $this->_password = $CI->session->userdata('password');
            }    
        }
    }
    function getUser(){return $this->_user;}
    function getPassword(){return $this->_password;}
    function login($user = "", $password = "")
    {
        if(empty($user)||empty($password))
            return FALSE;

        $CI =& get_instance();        

        $sql = "SELECT * FROM `usuarios` WHERE `user`='$user' AND `password`='$password'";
        $query = $CI->db->query($sql, array($user, $password));

        //login ok
        if($query->num_rows()==1)
        {
            $row = $query->row();

            $CI->session->set_userdata('user', $user);
            $this->_user = $user;
            $CI->session->set_userdata('password', $password);
            $this->_password = $row->password;

            $this->_auth = TRUE;

            return TRUE;
        }
        else
        {
            $this->_auth = FALSE;
            $this->logout();

            return FALSE;
        }
    }
    function logout()
    {
        $CI =& get_instance();
        $CI->session->sess_destroy();
        $this->_auth = FALSE;
    }
    function check()
    {
        if(!$this->_auth)
        {
            return FALSE;            
        }else{
        return TRUE;
    
        }
    }
}

?>



Call to a member function userdata() on a non-object - El Forum - 05-11-2011

[eluser]InsiteFX[/eluser]
This should be __construct not Centinela:
Code:
function __construct($auto = TRUE)
    {
        if($auto)
        {
            $CI =& get_instance();
            $CI->load->database();
            if($this->login($CI->session->userdata('user'), $CI->session->userdata('password')))
            {
                $this->_user = $CI->session->userdata('user');
                $this->_password = $CI->session->userdata('password');
            }    
        }
    }

InsiteFX


Call to a member function userdata() on a non-object - El Forum - 05-11-2011

[eluser]danmontgomery[/eluser]
Code:
$CI->load->library('session');



Call to a member function userdata() on a non-object - El Forum - 05-11-2011

[eluser]InsiteFX[/eluser]
@noctrum, I told him to autoload the session library, but not sure if he did!

InsiteFX


Call to a member function userdata() on a non-object - El Forum - 06-07-2011

[eluser]why.you.and.i[/eluser]
Hello, i'm new at CI and i'm having almost the same problem here. It's my error:

MY ERROR:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Hello::$Hello_model
Filename: admin/hello.php
Line Number: 15
Fatal error: Call to a member function getData() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/c4/application/controllers/admin/hello.php on line 21


MY CODE:
Here is my controller (hello.php)

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hello extends CI_Controller
{
   function __construct()
   {
      parent::__construct();
   }

  function index()
  {
    $this->load->model('hello');
    $data['result'] = $this->Hello_model->getData();
    $this->load->view('hello', $data);
  }
}


And my models (hello.php)
Code:
<?php
class Hello_model extends CI_Model
{
  function __construct()
  {
    parent::__construct();
  }

  function getData()
  {
    $query = $this->db->get('data');                  
    if($query->num_rows()==0) { /*SHOW SOME ERROR HERE*/ }
    else { return $query->result(); }
  }
}
?>


WHAT I'VE TRIED
If i change my model and call the function directly by using
Code:
$data['result'] = Hello_model::getData();
it works anyhow. And i have added to load database in my autoload. Here is my autoload:

application/config/autoload.php
Code:
$autoload['libraries'] = array('database');


Is there something i'm missing? Thank you for your time.


Call to a member function userdata() on a non-object - El Forum - 06-07-2011

[eluser]InsiteFX[/eluser]
You did not show your view file, but in your view file you will need to use a foreach loop to loop through the returned result set!

This is why your getting the error!
Code:
function index()
  {
    // this is wrong!
    $this->load->model('hello');

    // should be
    $this->load->model('hello_model');
    $data['result'] = $this->hello_model->getData();
    $this->load->view('hello', $data);
  }
}

InsiteFX