title: How to call database function into custom library in Codeigniter?
body: <p>I have created a library and am trying to access the database through it in codeigniter.</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  </code></pre>
  
  <p>class History {</p>
  
  <pre><code>function __construct() {
      //$CI-&gt;load-&gt;database();
      //$this-&gt;load-&gt;library('database');
      $CI =&amp; get_instance();
      $CI-&gt;load-&gt;database();
  }
  
  public function create_history($id){
      //$this-&gt;load-&gt;database();
      $sql = "INSERT INTO inventory_history SELECT null,i.* FROM inventory i where i.inventory_id = :id";
      $query = $this-&gt;CI-&gt;db-&gt;conn_id-&gt;prepare($sql);
      $query-&gt;bindParam(':id', $id, PDO::PARAM_INT);
      return $query-&gt;execute();
  }}
  </code></pre>
  
  <p>But I am getting error near the insert query. Going through Google I went around someone telling to use <code>$this-&gt;ci-&gt;db</code> to execute query. I'm not able to point out why there is error in database execution. How can I call database function into my custom library in codeigniter?</p>