Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6965487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:04:04+00:00 2026-05-27T16:04:04+00:00

I created a new model class as my_model.php inside /models folder, and a function

  • 0

I created a new model class as my_model.php inside /models folder, and a function inside it to load all elements:

function get_all(){
    $query = $this->db->get('test'); //test is my table 
   return $query->result();
}

In the controller, I instantiated the class and called the method;

$this->load->model('my_model');
$res = $this->my_model->get_all();

But, this throws me error saying:

Fatal error: Call to a member function get() on a non-object in /var/www/testapp/application/models/my_model.php on line 7

This line 7 points to the portion of the code where I have used $this->db. I tried to see the value of $db but I think it is magic accessor __get and __set, so I could not see the value of this property before calling that method.

I tried googling for several other cases but none of them match my scenarios and rather none of them could solve my problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:04:05+00:00Added an answer on May 27, 2026 at 4:04 pm

    You have to load the Database first

    $this->load->database();
    

    So, all code:

    function get_all(){
        $this->load->database();
        $query = $this->db->get('test'); //test is my table 
        return $query->result();
    }
    

    Or, load database in your __construct method.

    Or, IMO, It’s better to autoload database by changing application/config/autoload.php, example is below.

    $autoload['libraries'] = array('database','form_validation'); //form_validation is for example only
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to test that a UserProfile model is created as a new User
I'm calling a php function from my $.ajax call to add a new item
So this is how my startup model looks like: <?php class Test extends Zend_Db_Table{
I make services folder in my Zend project and i create new php file
I wanted to create a new property on a table in my model.. Basically
I have a model called Details, and two controller methods new and create. New
I've created new db via phpMyAdmin and while editing db privilages clicked delete localhost
I created a new SharePoint portal for testing purposes using a manually added HOSTS
I created a new email box for general support questions. When I try to
I created a new HalloWorld Makefile Project. There is a HalloWorld.cpp with my main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.