CodeIgniter Forums
Tutorial problems: "Call to a member function on a non-object" - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Tutorial problems: "Call to a member function on a non-object" (/thread-25713.html)



Tutorial problems: "Call to a member function on a non-object" - El Forum - 12-21-2009

[eluser]kyleb[/eluser]
Hey everyone,

I'm new to CI and the MVC structure, but I am looking forward to developing with it! I am following the excellent tutorials here:

http://codeigniter.com/tutorials/watch/blog/

And I'm having a bit of a problem loading any information from the database. I am using a pre-existing DB that I can connect to just fine (I have read-only access), scaffolding works, and I've done part one of the tutorial.

My controller code looks like this:
Code:
<?php

class Visits extends Controller {

    function Visits()
    {
        parent::Controller();

    }

    function index()
    {

        $data['title'] = "CodeIgniter Testing";
        $data['heading'] = "Testing Visit Table Access";
        $data['query'] = $this->db->get('VISIT');
        
        $this->load->view('visits_view',$data);
    }
}
?>

And my view code looks like this:

Code:
<h1>&lt;?=$heading?&gt;</h1>

&lt;?php foreach($query->result() as $row): ?&gt;

<h3>&lt;?=$row->title?&gt;</h3>

&lt;?php endforeach; ?&gt;

My problem is that when I attempt to view it at /index.php/visits, I get the following error:

"Fatal error: Call to a member function on a non-object in ...../codeigniter/system/application/controllers/visits.php on line 16

Line 16, of course, is:

$data['query'] = $this->db->get('VISIT');

I've quadruple checked it and Googled around - my code is identical to the screencast tutorial, yet it does not work at all. I am running some instance of PHP 4.x, though I don't think that matters.

The table name is in all caps (yes, I know - I'm not the DBA, I don't control these things) and I assure you it exists, as:
Code:
SELECT * FROM VISIT LIMIT 20, 10
Works just fine when I query the DB directly using SQLyog.

Thanks in advance! I know it's something small...


Tutorial problems: "Call to a member function on a non-object" - El Forum - 12-21-2009

[eluser]kyleb[/eluser]
So of course after trying one more thing, this worked (sort of):
Code:
$this->load->database();

Adding it to the Controller seemed to work; now my code looks like this:
Code:
&lt;?php

class Visits extends Controller {

    function Visits()
    {
        parent::Controller();

    }

    function index()
    {
        $this->load->database();

        $data['title'] = "CodeIgniter Testing";
        $data['heading'] = "Testing Visit Table Access";
        $data['query'] = $this->db->get('VISIT');
        
        $this->load->view('visits_view',$data);
    }
}
?&gt;

Now I'm getting:

Code:
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 23 bytes) in...

But that probably makes sense, given that the VISIT table is very large.

Was I missing something in my autoload.php or database.php file that necessitated the database class being instantiated from the controller?


Tutorial problems: "Call to a member function on a non-object" - El Forum - 12-21-2009

[eluser]jedd[/eluser]
[quote author="kyleb" date="1261468685"]
Code:
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 23 bytes) in...
[/quote]

This means you've got two problems.

The first, your php.ini file has a memory limit of 64mb.

The second is you are pulling in about 63mb more data than you should be from your database call.


For your db connection - either load the library as you've done (but you should really be pushing this into a model) or use the config/autoload.php


Tutorial problems: "Call to a member function on a non-object" - El Forum - 12-22-2009

[eluser]BrianDHall[/eluser]
For the memory limit you can up the limits in php.ini, or better yet use select or where to limit what the function is returning so you aren't asking CI to work through so much data.


Tutorial problems: "Call to a member function on a non-object" - El Forum - 12-22-2009

[eluser]saidai jagan[/eluser]
[quote author="BrianDHall" date="1261490557"]For the memory limit you can up the limits in php.ini.[/quote]
ini_set('memory_limit', '100M');