1. 程式人生 > >CI框架資料庫操作方法(參考)

CI框架資料庫操作方法(參考)

<?php
class demo_model extends CI_Model {
	public function __construct()
	{
		$this->load->database();
	}
	//查詢例項
	public function select()
	{	

		//SELECT * FROM mytable
		$query = $this->db->get('mytable');
		
		//SELECT * FROM mytable LIMIT 20, 10
		//說明:第二引數是每頁紀錄數,第三個引數是偏移
		$query = $this->db->get('mytable', 10, 20);
		
		//SELECT * FROM mytable where id=$id LIMIT  $offset,$limit
		$query = $this->db->get_where('mytable',array('id' => $id),$limit,$offset);
		
		//SELECT title, content, date FROM mytable
		$this->db->select('title, content, date');
		$query = $this->db->get('mytable');
		
		//SELECT MAX(age) as age FROM members
		$this->db->select_max('age');
		$query = $this->db->get('members');
		
		//SELECT MIN(age) as age FROM members
		$this->db->select_min('age');
		$query = $this->db->get('members');
		
		//SELECT MAX(age) as member_age FROM members
		$this->db->select_max('age', 'member_age');
		$query = $this->db->get('members');
		
		//SELECT SUM(age) as member_age FROM members
		$this->db->select_sum('age','member_age');
		$query = $this->db->get('members');
		
		//SELECT count(*) as total FROM members		
		$this->db->select('count(*) as total');
		$query = $this->db->get('members');
		
		//SELECT title, content, date FROM mytable
		$this->db->select('title, content, date');
		$this->db->from('mytable');
		$query = $this->db->get(); 
		
		//SELECT * FROM blogs
		//JOIN comments ON comments.id = blogs.id
		$this->db->select('*');
		$this->db->from('blogs');
		$this->db->join('comments', 'comments.id = blogs.id');
		$query = $this->db->get();
		
		//可選引數:left,right, outer, inner, left outer,right outer.
		//LEFT JOIN comments ON comments.id = blogs.id
		$this->db->join('comments', 'comments.id = blogs.id', 'left');

		//WHERE name = 'joe'
		$this->db->where('name','joe');
 
		//WHERE name = 'joe' and id=1
		$this->db->where('name','joe');
		$this->db->where('id',1);
		
		//WHERE name != 'joe' and id < 45
		$this->db->where('name !=', 'joe');
		$this->db->where('id <', 45); 

		//WHERE name = 'Joe' AND title = 'boss'
		$array = array('name' => $name, 'title' => $title);
		$this->db->where($array);
		
		//WHERE name != 'Joe' AND id<1 
		$array = array('name!=' => $name, 'id<' => $id);
		$this->db->where($array);
		
		//可以自定義where
		$where = "name='Joe' AND status='boss' OR status='active'";
		$this->db->where($where);
		
		//WHERE name != 'Joe' OR id > 50
		$this->db->where('name !=', $name);
		$this->db->or_where('id >', $id);
		
		//WHERE username IN ('Frank', 'Todd', 'James')
		$names = array('Frank', 'Todd', 'James');
		$this->db->where_in('username', $names);

		//OR username IN ('Frank', 'Todd', 'James')
		$names = array('Frank', 'Todd', 'James');
		$this->db->or_where_in('username', $names);
		
		//WHERE username NOT IN ('Frank', 'Todd', 'James')	
		$names = array('Frank', 'Todd', 'James');
		$this->db->where_not_in('username', $names);

		//OR username NOT IN ('Frank', 'Todd', 'James')
		$names = array('Frank', 'Todd', 'James');
		$this->db->or_where_not_in('username', $names);
		
		//WHERE title LIKE '%match%' AND body LIKE '%match%'
		$this->db->like('title', 'match');
		$this->db->like('body', 'match');
		
		//可選引數:before,after,both,none
		//WHERE title LIKE '%match' 
		$this->db->like('title', 'match', 'before'); 
		
		// WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%'
		$array = array('title' => $match, 'page1' => $match, 'page2' => $match);
		$this->db->like($array); 	
		
		// WHERE title LIKE '%match%' OR body LIKE '%match%'
		$this->db->like('title', 'match');
		$this->db->or_like('body', $match); 
		
		// WHERE title NOT LIKE '%match%
		$this->db->not_like('title', 'match');
		
		// WHERE title LIKE '%match%' OR body NOT LIKE '%match%'
		$this->db->like('title', 'match');
		$this->db->or_not_like('body', 'match'); 
		
		//GROUP BY title 
		$this->db->group_by("title"); 
		
		//GROUP BY title, date
		$this->db->group_by(array("title", "date")); 
		
		//ORDER BY title DESC 
		$this->db->order_by("title", "desc"); 
		
		//ORDER BY title DESC, name ASC 
		$this->db->order_by('title desc, name asc'); 
		
		//ORDER BY title DESC, name ASC 
		$this->db->order_by("title", "desc");
		$this->db->order_by("name", "asc");

		//LIMIT 10
		$this->db->limit(10);
		
		//LIMIT 20, 10 (僅限MySQL中。其它資料庫有稍微不同的語法)
		$this->db->limit(10, 20);
		
		//$this->db->count_all_results('my_table');	
	}
	//插入例項
	public function insert()
	{
		//INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
		$data = array('title' => 'My title' ,'name' => 'My Name' ,'date' => 'My date');
		$this->db->insert('mytable', $data); 

		//INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), 
		//('Another title', 'Another name', 'Another date')
		$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); 
		
		//INSERT INTO mytable (field) VALUES (field+1)
		$this->db->set('field', 'field+1', FALSE);
		$this->db->insert('mytable'); 

		//INSERT INTO mytable (field) VALUES ('field+1')
		//$this->db->set('wealth', 'wealth-'.$wealth, FALSE);
		$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()
	{
		// UPDATE mytable 
		// SET title = '{$title}', name = '{$name}', date = '{$date}'
		// WHERE id = $id
		$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");
		
		//
		$where = array('id' => $id);
		$this->db->update('mytable', $data, $where);
	}
	//刪除例項
	public function delete()
	{
		//DELETE FROM mytable WHERE id = $id
		$this->db->delete('mytable', array('id' => $id));
		
		//DELETE FROM table1 WHERE id = 5
		//DELETE FROM table2 WHERE id = 5
		//DELETE FROM table3 WHERE id = 5
		$tables = array('table1', 'table2', 'table3');
		$this->db->where('id', '5');
		$this->db->delete($tables);
		
		// DELETE FROM mytable
		$this->db->empty_table('mytable'); 
	}
	//其他例項
	public function other()
	{
		//鏈式方法
		$this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20);
		$query = $this->db->get();
		
		//Active Record 快取
		$this->db->start_cache();
		$this->db->select('field1');
		$this->db->stop_cache();

		$this->db->get('tablename');

		//此處輸出:SELECT `field1` FROM (`tablename`)

		$this->db->select('field2');
		$this->db->get('tablename');

		//此處輸出:SELECT `field1`, `field2` FROM (`tablename`)

		$this->db->flush_cache();

		$this->db->select('field2');
		$this->db->get('tablename');
		//此處輸出:SELECT `field2` FROM (`tablename`)
	}
	//顯示結果
	public function result()
	{
		//原生寫法
		$sql="select * from user";
        $result=$this->db->query($sql)->result_array();
		
		
		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');
		
		//顯示插入id
		return $this->db->insert_id();
		
		//顯示my_table有多少行資料
		return $this->db->count_all('my_table');
		
		//顯示系統使用的資料庫
		return $this->db->platform();
		
		//輸出系統正在執行的資料庫版本號 
		return $this->db->version();
		
		//顯示最近執行的SQL語句:SELECT * FROM sometable.... 
		return $this->db->last_query();
		
		//生成一個SQL語句
		//INSERT INTO table_name (name, email, url) VALUES ('Rick', '
[email protected]
', 'example.com') $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); } }