title: How to load a model from a custom library in CodeIgniter to check if values are existing in the database?
body: <p>I'm currently working with REST API's in CodeIgniter to display the location details of the user based on it's IP address. The problem is, if the user's IP address is existing, it will just be retrieved from the database table. Otherwise if the user's IP address is new, it will be called from the response of the REST API and save it to the database table to minimize the number of API calls</p>
  
  <p>I already tried to use the ipstack API to fetch the results and used them on the controller and view. However, if there will be a large number of users, the API request will be limited because I am just using the free version of the API. I also tried to create a database table in order to insert the values being retrieved by the API and later just select the values in order to minimize the API calls. I also tried to create a custom model in order to be called in the API library which I'd created to be called by the API, but when I am loading the model to the library, it is giving me errors.</p>
  
  <p>I created a <code>user_location</code> table in order to save the values being retrieved by the API and later display them to minimize the API calls. <a href="https://i.stack.imgur.com/QJK7k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QJK7k.png" alt="Here is the table that I created in order to save the fetched data being retrieved by the REST API"></a></p>
  
  <p>Here is my code for the custom model that I created in order to save and fetch the API results in the database:</p>
  
  <pre><code>class Ipstack_model extends CI_Model
  
  {
  
  function __construct()
  {
  
      parent::__construct();
  }
  
  /*
      @Description : Select all the fields from the user_location table
      @Author      : Aristotle Doria
      @Output      :
      @Created Date: 13-02-2019
  */
  function get_user_location($table)
  {
      $this-&gt;db-&gt;select("*");
      $this-&gt;db-&gt;from($table);
  
      $query = $this-&gt;db-&gt;get();
  
      return $query-&gt;result();
  }
  
  
  /*
      @Description  : Insert values from the user_location table
      @Author       : Aristotle Doria
      @Output       :
      @Created Date : 13-02-2019 
  */
  function insert_user_location($table, $data)
  {
      $this-&gt;db-&gt;insert($table, $data);
  }
  
  }
  </code></pre>
  
  <p>Here is my for the library that I made in order to call the ipstack API using cURL. I am trying to call the model that I created in the library in order to check for the values of REST API in the database, but I'm a little bit stuck with this part and giving me some errors.:</p>
  
  <pre><code>class Ipstack {
  
  public function __construct()
  {
      $CI =&amp; get_instance();
  }
  
  public function fetch_location($ip_address)
  {
  
      $CI-&gt;load-&gt;model('Ipstack_model');
  
      $this-&gt;db-&gt;where('user_ip_address', $ip_address);
  
      $query = $this-&gt;db-&gt;get('user_ip_address');
  
          if($query-&gt;num_rows() &gt; 0)
          {
              $this-&gt;db-&gt;get_user_location();
          }
          else
          {
              //set IP address and API access key
              $ip = $_SERVER['REMOTE_ADDR'];
              if( ENVIRONMENT == 'local' ) $ip = '112.211.220.109';
              $access_key = 'e88d4f685c07d3dae06bd952cd7e3c62';
              $curl_url = 'http://api.ipstack.com/'.$ip.'?access_key='.$access_key.'';
  
              //Initialize CURL
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              curl_setopt($ch, CURLOPT_URL, $curl_url);
  
              //store the data;
              $json = curl_exec($ch);
              curl_close($ch);
          }
              //Decode JSON response;
              $api_result = json_decode($json, true);
              return $api_result;
  }
  
  }
  </code></pre>
  
  <p>Here are the errors that I am getting:</p>
  
  <p><a href="https://i.stack.imgur.com/0ulGz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0ulGz.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/uwdKF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uwdKF.png" alt="enter image description here"></a></p>