I have created a library and am trying to access the database through it in codeigniter.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class History {
function __construct() {
//$CI->load->database();
//$this->load->library('database');
$CI =& get_instance();
$CI->load->database();
}
public function create_history($id){
//$this->load->database();
$sql = "INSERT INTO inventory_history SELECT null,i.* FROM inventory i where i.inventory_id = :id";
$query = $this->CI->db->conn_id->prepare($sql);
$query->bindParam(':id', $id, PDO::PARAM_INT);
return $query->execute();
}}
But I am getting error near the insert query. Going through Google I went around someone telling to use $this->ci->db 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?