CodeIgniter Forums
Fatal error: Call to a member function on a non-object on line 14, on blog controller page - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Fatal error: Call to a member function on a non-object on line 14, on blog controller page (/thread-13673.html)



Fatal error: Call to a member function on a non-object on line 14, on blog controller page - El Forum - 12-01-2008

[eluser]Brad Morse[/eluser]
I am doing the Weblog tutorial and I have scanned all the code and everything is exact.

The scaffolding part worked, I entered a couple entries, so it connects to the db just fine, but I get this error:

Fatal error: Call to a member function on a non-object in (path URL) on line 14

This is line 14:
Code:
$data['query'] = $this->db->get('entries');

Here is the blog controller page:

Code:
<?php
    
class Blog extends Controller {

    function Blog() {
        parent::Controller();
        
        //$this->load->scaffolding('entries');
    }
    
    function index() {
        $data['title'] = "My Blog Title";
        $data['heading'] = "My Blog Heading";
        $data['query'] = $this->db->get('entries');
        
        $this->load->view('blog_view', $data);
    }
}

?>

Here is the blog view page:

Code:
<html>
<head>
<title><?=$title?></title>
</head>
<body>
    
    <h1>&lt;?=$heading?&gt;</h1>
    
    &lt;?php foreach($query->result() as $row): ?&gt;
        <h3>&lt;?=$row->title?&gt;</h2>
        <p>&lt;?=$row->body?&gt;</p>
        <hr>
    &lt;?php endforeach; ?&gt;
    
&lt;/body&gt;
&lt;/html&gt;

Any help is appreciated.


Fatal error: Call to a member function on a non-object on line 14, on blog controller page - El Forum - 12-02-2008

[eluser]zac[/eluser]
I haven't tested this myself, but it looks like maybe the DB library isn't loaded. Try loading it in your controller, like this:

Code:
$this->load->database();

If that fixes the problem, then you can make the DB library always load automatically by adding it to
Code:
application/config/autoload.php
(as explained here: http://ellislab.com/codeigniter/user-guide/database/connecting.html)

-Zac


Fatal error: Call to a member function on a non-object on line 14, on blog controller page - El Forum - 12-02-2008

[eluser]Brad Morse[/eluser]
I added that line to autoload.php, and it worked

Thank you.