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);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_33729083/article/details/83994120