<?php
class demo_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function select()
{
$query = $this->db->get('mytable');
$query = $this->db->get('mytable', 10, 20);
$query = $this->db->get_where('mytable',array('id' => $id),$limit,$offset);
$this->db->select('title, content, date');
$query = $this->db->get('mytable');
$this->db->select_max('age');
$query = $this->db->get('members');
$this->db->select_min('age');
$query = $this->db->get('members');
$this->db->select_max('age', 'member_age');
$query = $this->db->get('members');
$this->db->select_sum('age','member_age');
$query = $this->db->get('members');
$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
$this->db->join('comments', 'comments.id = blogs.id', 'left');
$this->db->where('name','joe');
$this->db->where('name','joe');
$this->db->where('id',1);
$this->db->where('name !=', 'joe');
$this->db->where('id <', 45);
$array = array('name' => $name, 'title' => $title);
$this->db->where($array);
$array = array('name!=' => $name, 'id<' => $id);
$this->db->where($array);
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);
$this->db->where('name !=', $name);
$this->db->or_where('id >', $id);
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
$names = array('Frank', 'Todd', 'James');
$this->db->or_where_in('username', $names);
$names = array('Frank', 'Todd', 'James');
$this->db->where_not_in('username', $names);
$names = array('Frank', 'Todd', 'James');
$this->db->or_where_not_in('username', $names);
$this->db->like('title', 'match');
$this->db->like('body', 'match');
$this->db->like('title', 'match', 'before');
$array = array('title' => $match, 'page1' => $match, 'page2' => $match);
$this->db->like($array);
$this->db->like('title', 'match');
$this->db->or_like('body', $match);
$this->db->not_like('title', 'match');
$this->db->like('title', 'match');
$this->db->or_not_like('body', 'match');
$this->db->group_by("title");
$this->db->group_by(array("title", "date"));
$this->db->order_by("title", "desc");
$this->db->order_by('title desc, name asc');
$this->db->order_by("title", "desc");
$this->db->order_by("name", "asc");
$this->db->limit(10);
$this->db->limit(10, 20);
}
public function insert()
{
$data = array('title' => 'My title' ,'name' => 'My Name' ,'date' => 'My date');
$this->db->insert('mytable', $data);
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
$this->db->insert_batch('mytable', $data);
$this->db->set('field', 'field+1', FALSE);
$this->db->insert('mytable');
$this->db->set('field', 'field+1');
$this->db->insert('mytable');
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->set($array);
$this->db->insert('mytable');
}
public function update()
{
$data = array( 'title' => $title,'name' => $name,'date' => $date);
$this->db->where('id', $id);
$this->db->update('mytable',$data);
$this->db->update('mytable', $data, "id = 4");
$this->db->where($where)->set('views','views+1',false)->update('news_info');
$where = array('id' => $id);
$this->db->update('mytable', $data, $where);
}
public function delete()
{
$this->db->delete('mytable', array('id' => $id));
$tables = array('table1', 'table2', 'table3');
$this->db->where('id', '5');
$this->db->delete($tables);
$this->db->empty_table('mytable');
}
public function other()
{
$this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20);
$query = $this->db->get();
$this->db->start_cache();
$this->db->select('field1');
$this->db->stop_cache();
$this->db->get('tablename');
$this->db->select('field2');
$this->db->get('tablename');
$this->db->flush_cache();
$this->db->select('field2');
$this->db->get('tablename');
}
public function result()
{
return $query->row_array();
return $query->result_array();
return $query->row();
return $query->result();
return $query->row_array(4);
return $query->num_rows();
return $query->num_fields();
$this->db->affected_rows();
return $this->db->query('sql');
return $this->db->insert_id();
return $this->db->count_all('my_table');
return $this->db->platform();
return $this->db->version();
return $this->db->last_query();
$data = array('name' => $name, 'email' => $email, 'url' => $url);
return $this->db->insert_string('table_name', $data);
$data = array('name' => $name, 'email' => $email, 'url' => $url);
$where = "author_id = 1 AND status = 'active'";
return $this->db->update_string('table_name', $data, $where);
$sql = "";
$result=$this->db->query($sql)->result_array();
}
}