Welcome Guest, Not a member yet? Register   Sign In
Fatal error: Call to a member function get() on a non-object in
#1

[eluser]rajendiran[/eluser]
Friends,

I am getting the following error when i am trying to connect with database.
Fatal error: Call to a member function get() on a non-object in

this is my source code in blog.php\
<?php
class Blog extends CI_Controller
{
function index()
{
$data['title'] = "My Blog Title";
$data['header'] = "My Blog Heading";
$data['interest'] = array("cricket","chees");
$data['query'] = $this->db->get('entries');
$this->load->view("blog_view", $data);
}
}
?>
blog_view.php code is
<?php
foreach ($query->result() as $row): ?>
<h3>&lt;?php echo $row->title; ?&gt; </h3>
<p>&lt;?php echo $row->body; ?&gt;</p>

&lt;?php endforeach; ?&gt;

please help me solve this error.

Thank you
Rajendiran A
#2

[eluser]Sudz[/eluser]
You don't have access to $this->db->get('entries'); in controller.
write your query in model not in controller.
#3

[eluser]CodeIgniteMe[/eluser]
[quote author="Sudhakar@CI" date="1309281955"]You don't have access to $this->db->get('entries'); in controller.
write your query in model not in controller.[/quote]

You can actually access $this->db in your controllers, but is just not the right way to do it, since we use MVC. I suggest you try loading your database library first. either manually,
Code:
&lt;?php
class Blog extends CI_Controller
{
  function index()
  {
      $this->load->database(); // add this line
      $data[‘title’] = “My Blog Title”;
      $data[‘header’] = “My Blog Heading”;
      $data[‘interest’] = array(“cricket”,“chees”);
      $data[‘query’] = $this->db->get(‘entries’);
      $this->load->view(“blog_view”, $data);
  }
}
?&gt;
or auto-laoding it in your autoload.php config file. Also check your database.php configuration.
#4

[eluser]toopay[/eluser]
Load your database first in your constructor (so it is reusable for other function) or autoload it in your autoload.php under config folder
Code:
function __construct()
{
   parent::__construct();
   $this->load->database();
}
also remove the closing tag '?&gt;'!
#5

[eluser]viktors[/eluser]
Hello. Found the same error.
I tried to do so:
1. initialize db in avtoload
2. initsializirovta dB in the constructor model with an error: Fatal error: Call to a member function database () on a non-object in ... \ application \ models \ test.php on line 7
3. initialize the db in the function.

The error only occurs in the model. get () function works in the controller and helper.

Also feature works if you use this code:

Code:
function test1 ()
{
$CI = & get_instance ();
$q = $CI->db->get('article_content');
return$q->num_rows();
}


Tested on two versions of Apache result is the same.
What can you recommend?
#6

[eluser]toopay[/eluser]
In your model, you should put contructor properly
Code:
// Constructor in model
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        // Then load the database
        $this->load->database();
    }
#7

[eluser]viktors[/eluser]
Dont work. Call this error: Fatal error: Call to a member function database () on a non-object in ... \ application \ models \ test.php on line 7 (
#8

[eluser]CodeIgniteMe[/eluser]
I recommend autoloading the database class in the config file, so you won't keep in mind to always load the database on every controller or model. Also, using this:
Code:
function test1 ()
{
$CI = & get_instance ();
$q = $CI->db->get('article_content');
return$q->num_rows();
}
is dangerous because you are calling the CI superobject by reference.
#9

[eluser]CodeIgniteMe[/eluser]
Could you post your model class here?
#10

[eluser]viktors[/eluser]
autoload.php

Code:
...

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

...

controller

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    
    public function index()
    {
        
        $this->load->model('test');
            
        echo $this->test->test1();

    }
}

model

Code:
&lt;?php
class Test extends CI_Model {

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

    }

    function test1()
    {    
        $q=$this->db->get('article_content');
        return  $q->num_rows();
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB