Fatal error: Call to a member function 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: Fatal error: Call to a member function on a non-object (/showthread.php?tid=2549) |
Fatal error: Call to a member function on a non-object - El Forum - 08-12-2007 [eluser]hellboy[/eluser] Hi Everyone, Been using Ci for a couple of days now, and I'm pretty impressed so far, but seem to be stuck with a problem 'Fatal error: Call to a member function on a non-object' in one of my views. From what I can see the connection to the DB seems ok and the query is returning data. Here's some code: The Controller Code: <?php The View Code: <html> The Model Code: <?php The echo lines in the model there tell me that 2 rows are being returned form the query (which is spot on!), but I just can't seem to use the $query in the view properly. I'm using $autoload['libraries'] = array('database'); in my autoload.php file and scaffolding seems to work fine, so I'd guess there's no problem in the database.php file. Any advice greatly appreciated! Fatal error: Call to a member function on a non-object - El Forum - 08-12-2007 [eluser]hellboy[/eluser] Just a quick update. I changed the following in my controller: Code: $data['query'] = $this->Articles_model->get_last_ten_articles(); Code: $data['query'] = $this->db->get('articles'); Any ideas? Fatal error: Call to a member function on a non-object - El Forum - 08-12-2007 [eluser]FrankieShakes[/eluser] hellboy, The error is actually being caused in this line of your view: Code: <?php foreach($query->result() as $row): ?> If you look at the model's "get_last_ten_articles()" method, your return is the actual "result set", so you would either change the view to be: Code: <?php foreach($query as $row): ?> or, rather than returning the result set in the model, you could do: Code: function get_last_ten_articles() Fatal error: Call to a member function on a non-object - El Forum - 08-12-2007 [eluser]hellboy[/eluser] Thank you very much. It works prefectly now! Fatal error: Call to a member function on a non-object - El Forum - 08-12-2007 [eluser]FrankieShakes[/eluser] Anytime! Glad it worked out for you. |