view raw
K Cloud K Cloud - 9 months ago 172
PHP Question

How to call database function into custom library in Codeigniter?

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 =& get_instance();

public function create_history($id){
$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
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?


is it this?

class YourLibName {
   private $CI;

   function __construct() {
       $this->CI =& get_instance();


Update: I think you should not load and use database object in library. You better create a model and call it. A library should be independent and can be used with other application.