phpcms 数据库操作总结

数据库的其他类继承的都是libs/class/model.class.php

这里面有写好的操作数据库的常用方法

1.增
insert($data, $return_insert_id = false, $replace = false)
/**
	 * 执行添加记录操作
	 * @param $data 		要增加的数据,参数为数组。数组key为字段值,数组值为数据取值
	 * @param $return_insert_id 是否返回新建ID号
	 * @param $replace 是否采用 replace into的方式添加数据
	 * @return boolean
	 */
	
     insert($data, $return_insert_id = false, $replace = false)

 union_listinfo($main_table, $secondary_table, $fields = '*', $where = '', $order = '', $page = 1, $pagesize = 20, $union_on='m.id=s.id',$limit='')

/**
	 * 两个表联合查询分页
	 * @param string $main_table 主表
	 * @param string $secondary_table 副表
	 * @param string $data 查询字段
	 * @param string $where 条件
	 * @param string $order 排序
	 * @param string $page 分页
	 * @param string $pagesize 每页记录数
	 * @return array
	 * @author:hans
	 */
         union_listinfo($main_table, $secondary_table, $fields = '*', $where = '', $order = '', $page = 1, $pagesize = 20, $union_on='m.id=s.id',$limit='')     

 2.删除

delete($where) $where is array

	/**
	 * 执行删除记录操作
	 * @param $where 		删除数据条件,不充许为空。
	 * @return boolean
	 */
	delete($where)

   asdd

3.改

 update($data,$where)

/**
	 * 执行更新记录操作
	 * @param $data 		要更新的数据内容,参数可以为数组也可以为字符串,建议数组。
	 * 						为数组时数组key为字段值,数组值为数据取值
	 * 						为字符串时[例:`name`='phpcms',`hits`=`hits`+1]。
	 *						为数组时[例: array('name'=>'phpcms','password'=>'123456')]
	 *						数组的另一种使用array('name'=>'+=1', 'base'=>'-=1');程序会自动解析为`name` = `name` + 1, `base` = `base` - 1
	 * @param $where 		更新数据时的条件,可为数组或字符串
	 * @return boolean
	 */
	 update($data, $where = '')
		

 

4.查

select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='')
	/**
	 * 执行sql查询
	 * @param $where 		查询条件[例`name`='$name']
	 * @param $data 		需要查询的字段值[例`name`,`gender`,`birthday`]
	 * @param $limit 		返回结果范围[例:10或10,10 默认为空]
	 * @param $order 		排序方式	[默认按数据库默认方式排序]
	 * @param $group 		分组方式	[默认为空]
	 * @param $key          返回数组按键名排序
	 * @return array		查询结果集数组
	 */
	final public function select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='') {
		if (is_array($where)) $where = $this->sqls($where);
		return $this->db->select($data, $this->table_name, $where, $limit, $order, $group, $key);
	}
get_one($where = '', $data = '*', $order = '', $group = '')
/**
	 * 获取单条记录查询
	 * @param $where 		查询条件
	 * @param $data 		需要查询的字段值[例`name`,`gender`,`birthday`]
	 * @param $order 		排序方式	[默认按数据库默认方式排序]
	 * @param $group 		分组方式	[默认为空]
	 * @return array/null	数据查询结果集,如果不存在,则返回空
	 */
	final public function get_one($where = '', $data = '*', $order = '', $group = '') {
		if (is_array($where)) $where = $this->sqls($where);
		return $this->db->get_one($data, $this->table_name, $where, $order, $group);
	}
query($sql) 用query来执行sql,得到的结果需要遍历一次才是数组
/**
	 * 直接执行sql查询
	 * @param $sql							查询sql语句
	 * @return	boolean/query resource		如果为查询语句,返回资源句柄,否则返回true/false
	 */
	final public function query($sql) {
		$sql = str_replace('phpcms_', $this->db_tablepre, $sql);
		return $this->db->query($sql);
	}
 
listinfo($where = '', $order = '', $page = 1, $pagesize = 20, $key='', $setpages = 10,$urlrule = '',$array = array(), $data = '*')查询多条记录并且分页,这里用到的分页和后台的分页是相同的样式(到后台看操作日志的分页),如果需要修改样式的话,需要赋值框架自己的分页,重命名后再修改,网上有很多例子。
/**
	 * 查询多条数据并分页
	 * @param $where
	 * @param $order
	 * @param $page
	 * @param $pagesize
	 * @return unknown_type
	 */
	final public function listinfo($where = '', $order = '', $page = 1, $pagesize = 20, $key='', $setpages = 10,$urlrule = '',$array = array(), $data = '*') {
		$where = to_sqls($where);
		$this->number = $this->count($where);
		$page = max(intval($page), 1);
		$offset = $pagesize*($page-1);
		$this->pages = pages($this->number, $page, $pagesize, $urlrule, $array, $setpages);
		$array = array();
		if ($this->number > 0) {
			return $this->select($where, $data, "$offset, $pagesize", $order, '', $key);
		} else {
			return array();
		}
	}
union_listinfo($main_table, $secondary_table, $fields = '*', $where = '', $order = '', $page = 1, $pagesize = 20, $union_on='m.id=s.id',$limit='')多表链接查询,类似tp里面的leftjoin,innerjoin 一般用于主表和附表链
	public function union_listinfo($main_table, $secondary_table, $fields = '*', $where = '', $order = '', $page = 1, $pagesize = 20, $union_on='m.id=s.id',$limit='') {
		/*获取记录总数*/
		$query_count = "SELECT count(*) AS `total` FROM $main_table AS m INNER JOIN $secondary_table AS s ON $union_on WHERE $where";
		$this->db->query($query_count);
		$counts = $this->db->fetch_next();
		$this->number = $counts['total'];
		/*设置分页*/
		$page = max(intval($page), 1);
		$offset = $pagesize*($page-1);
		$this->pages = pages($this->number, $page, $pagesize);
		/*获取记录*/
		$array = array();
		if ($this->number > 0) {
			$query = "SELECT $fields FROM $main_table AS m";
			$query .= " INNER JOIN $secondary_table AS s ON $union_on";
			$query .= " WHERE $where";
			if($order) $query .= " ORDER BY $order";//排序
			if($limit) $query .= " LIMIT $limit";
			else $query .= " LIMIT $offset, $pagesize"; 
			$this->db->query($query);
			$data = array();
			while($r = $this->db->fetch_next()) {
				$data[] = $r;		
			}
			return $data;
		} else {
			return array();
		}
	}

 

其他查询 :

count($where = '')
/**
	 * 计算记录数
	 * @param string/array $where 查询条件
	 */
	final public function count($where = '') {
		$r = $this->get_one($where, "COUNT(*) AS num");
		return $r['num'];
	}

 insert_id()

/**
	 * 获取最后一次添加记录的主键号
	 * @return int 
	 */
	final public function insert_id() {
		return $this->db->insert_id();
	}

 affected_rows()

 get_primary()

get_fields($table_name = '')

table_exists($table)

field_exists($field)

fetch_array()

	/**
	 * 获取最后数据库操作影响到的条数
	 * @return int
	 */
	final public function affected_rows() {
		return $this->db->affected_rows();
	}
	
	/**
	 * 获取数据表主键
	 * @return array
	 */
	final public function get_primary() {
		return $this->db->get_primary($this->table_name);
	}
	
	/**
	 * 获取表字段
	 * @param string $table_name    表名
	 * @return array
	 */
	final public function get_fields($table_name = '') {
		if (empty($table_name)) {
			$table_name = $this->table_name;
		} else {
			$table_name = $this->db_tablepre.$table_name;
		}
		return $this->db->get_fields($table_name);
	}
	
	/**
	 * 检查表是否存在
	 * @param $table 表名
	 * @return boolean
	 */
	final public function table_exists($table){
		return $this->db->table_exists($this->db_tablepre.$table);
	}
	
	/**
	 * 检查字段是否存在
	 * @param $field 字段名
	 * @return boolean
	 */
	public function field_exists($field) {
		$fields = $this->db->get_fields($this->table_name);
		return array_key_exists($field, $fields);
	}
	
	final public function list_tables() {
		return $this->db->list_tables();
	}
	/**
	 * 返回数据结果集
	 * @param $query (mysql_query返回值)
	 * @return array
	 */
	final public function fetch_array() {
		$data = array();
		while($r = $this->db->fetch_next()) {
			$data[] = $r;		
		}
		return $data;
	}
	

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值