Skip to content

Instantly share code, notes, and snippets.

@jQsafi
Last active March 5, 2017 21:10
Show Gist options
  • Select an option

  • Save jQsafi/28cef482993d9dd4fa05 to your computer and use it in GitHub Desktop.

Select an option

Save jQsafi/28cef482993d9dd4fa05 to your computer and use it in GitHub Desktop.
An useful library
<?php
/*
* Database_operation library
* Shafayat Hossain
*
*
*
* Version 1.1
*
* Copyright 2015 HnH Soft
*/
class Database_operation
{
private $CI;
function __construct()
{
$this->CI = & get_instance();
}//end of fucntion
function insert_data($insertData=array(),$tablename='')
{
$datainserted=1;
try
{
$result_insert=$this->CI->db->insert($tablename,$insertData);
if($result_insert!=TRUE)
throw new Custom_exception($this->CI->lang->line('exception.insertrror')."#Error Message-->".$this->CI->db->_error_message()."#Last query-->".$this->CI->db->last_query());
if($result_insert==TRUE)
return $datainserted;
}
catch (Custom_exception $e)
{
$e->process_error();
die();
}
}//end of function
function insert_update_data($conditions=array(),$data=array(),$tablename='')
{
$datainserted=1;
$is_there = $this->select_data($tablename,$conditions,key($conditions));
if($is_there->num_rows()<=0)
$datainserted = $this->insert_data($data,$tablename);
else
$datainserted = $this->update_data($conditions,$data,$tablename);
return $datainserted;
}//end of function
function update_data($conditions,$editdata,$tablename='')
{
$dataupdated=1;
if(count($conditions)>0)
$this->CI->db->where($conditions);
try
{
$result_update=$this->CI->db->update($tablename,$editdata);
if($result_update!=TRUE)
throw new Custom_exception($this->CI->lang->line('exception.updaterror')."#Error Message-->".$this->CI->db->_error_message()."#Last query-->".$this->CI->db->last_query());
if($result_update==TRUE)
return $dataupdated;
}
catch (Custom_exception $e)
{
$e->process_error();
die();
}
}//end of function
function delete_data($conditions=array(),$tablename='')
{
$datadeleted=1;
if(count($conditions)>0)
$this->CI->db->where($conditions);
try
{
$result_delete=$this->CI->db->delete($tablename);
if($result_delete!=TRUE)
throw new Custom_exception($this->CI->lang->line('exception.deleterror')."#Error Message-->".$this->CI->db->_error_message()."#Last query-->".$this->CI->db->last_query());
if($result_delete==TRUE)
return $datadeleted;
}
catch (Custom_exception $e)
{
$e->process_error();
die();
}
}//end of function
function select_data($tablename='',$conditions=array(),$selectitem='',$join_array=array(),$like_array=array(),$orderby_array=array(),$custom_where='',$is_distinct='',$limit='',$offset='',$groupby='')
{
if(count($conditions)>0)
$this->CI->db->where($conditions);
if($custom_where!='')
{
$this->CI->db->where($custom_where,NULL,FALSE);
$this->CI->db->select($selectitem,FALSE);
}
else
$this->CI->db->select($selectitem,FALSE);
$this->CI->db->from($tablename);
if($is_distinct==1)
$this->CI->db->distinct();
if(count($join_array)>0)
{
foreach($join_array as $key=>$value)
{
$values = explode("#",$value);
if($values[1]=='left')
$this->CI->db->join($key,$values[0],'left');
else
$this->CI->db->join($key,$values[0]);
}
}//end of if
if(count($like_array)>0)
{
foreach($like_array as $key=>$value)
{
$this->CI->db->like($key,$value,'both');
}
}//end of if
if(count($orderby_array)>0)
{
foreach($orderby_array as $key=>$value)
{
$this->CI->db->order_by($key,$value);
}
}//end of if
if($groupby!='')
{
$this->CI->db->group_by($groupby);
}
if($limit!='' && $offset!='')
$this->CI->db->limit($limit,$offset);
if($limit!='' && $offset=='')
$this->CI->db->limit($limit);
try
{
$result = $this->CI->db->get();
if(!$result)
throw new Custom_exception($this->CI->lang->line('exception.fetchingerror')."#Error Message-->".$this->CI->db->_error_message()."#Last query-->".$this->CI->db->last_query());
}
catch (Custom_exception $e)
{
$e->process_error();
die();
}
return $result;
}//end of function
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment