March 18, 2012

How to load the db class after the session in CodeIgniter

Question by yeah its me

when i want to make a foreach($_SESSION[‘banners’]->result() as $banner), i get this error:

The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "CI_DB_mysql_result" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition in….

how can i load the db class after the session, i am using the native session $_SESSION, i dont want to use the codeigniter sessions because they have problems in IE, how can i resolve this problem?? or what function i need to use to load the DB class after the session in CI?

Answer by Starx

Use the native Codeigniter Session, to avoid such problems. To fix the problem with IE, make the following changes on config.php

$config['sess_cookie_name']        = 'ci_session'; 

to

$config['sess_cookie_name']        = 'cisession';

  • Initiate the library like this $this->load->library('session');

  • Set data in the session like this $this->session->set_userdata('item', 'value');

  • And read the values like $this->session->userdata('item');

Then on your application you will able to use it like this

foreach($this->session->userdata('banners') ->result() as $banner) {}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!