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

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

1.增

  insert($data, $return_insert_id = false, $replace = false)
  1. /**
  2. * 执行添加记录操作
  3. * @param $data 要增加的数据,参数为数组。数组key为字段值,数组值为数据取值
  4. * @param $return_insert_id 是否返回新建ID号
  5. * @param $replace 是否采用 replace into的方式添加数据
  6. * @return boolean
  7. */
  8.  
  9. 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='')

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

2.删除

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

3.改

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

4.查

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

insert_id()

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

 affected_rows()

 get_primary()

get_fields($table_name = '')

table_exists($table)

field_exists($field)

fetch_array()

  1. /**
  2. * 获取最后数据库操作影响到的条数
  3. * @return int
  4. */
  5. final public function affected_rows() {
  6. return $this->db->affected_rows();
  7. }
  8.  
  9. /**
  10. * 获取数据表主键
  11. * @return array
  12. */
  13. final public function get_primary() {
  14. return $this->db->get_primary($this->table_name);
  15. }
  16.  
  17. /**
  18. * 获取表字段
  19. * @param string $table_name 表名
  20. * @return array
  21. */
  22. final public function get_fields($table_name = '') {
  23. if (empty($table_name)) {
  24. $table_name = $this->table_name;
  25. } else {
  26. $table_name = $this->db_tablepre.$table_name;
  27. }
  28. return $this->db->get_fields($table_name);
  29. }
  30.  
  31. /**
  32. * 检查表是否存在
  33. * @param $table 表名
  34. * @return boolean
  35. */
  36. final public function table_exists($table){
  37. return $this->db->table_exists($this->db_tablepre.$table);
  38. }
  39.  
  40. /**
  41. * 检查字段是否存在
  42. * @param $field 字段名
  43. * @return boolean
  44. */
  45. public function field_exists($field) {
  46. $fields = $this->db->get_fields($this->table_name);
  47. return array_key_exists($field, $fields);
  48. }
  49.  
  50. final public function list_tables() {
  51. return $this->db->list_tables();
  52. }
  53. /**
  54. * 返回数据结果集
  55. * @param $query (mysql_query返回值)
  56. * @return array
  57. */
  58. final public function fetch_array() {
  59. $data = array();
  60. while($r = $this->db->fetch_next()) {
  61. $data[] = $r;
  62. }
  63. return $data;
  64. }

phpcms 操作数据库 增删改查的更多相关文章

  1. 2. MongoDB基本操作 —— 用Mongo.exe操作数据库增删改查

    一.开篇 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象 ...

  2. Django-Model操作数据库(增删改查、连表结构)

    一.数据库操作 1.创建model表         基本结构 1 2 3 4 5 6 from django.db import models     class userinfo(models.M ...

  3. MongoDB学习day04--NodeJs操作数据库增删改查

    一.在Nodejs中使用Mongodb Nodejs需要引入的包 npm install mongodb --save -dev 或者使用镜像 cnpm install mongodb --save ...

  4. java操作数据库增删改查的小工具1--TxQueryRunner

    在java程序中,一般使用jdbc连接数据库,比较麻烦,在看传智教程时学了一个工具类,用于简化与数据库之间的操作步骤,就是TxQueryRunner,他是QueryRunner的子类,用起来和他是一样 ...

  5. JavaWeb学习记录(七)——MVC操作数据库增删改查与分页功能

    一.分页工具类 package blank.util;import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; ...

  6. java操作数据库增删改查的小工具2--TxQueryRunner

    当涉及到多表查询时,如数据库中有两张表分别为t_person和t_address,表结构如下: 其中t_person的外键为t-address的主键aid, 新建两个javaBean类,Person ...

  7. 前端web通过flask操作数据库-增删改查

    后端python代码: #coding:utf8 from flask import Flask,request,render_template import pymysql as mysql imp ...

  8. python操作mysql数据库增删改查的dbutils实例

    python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...

  9. Yii2.0高级框架数据库增删改查的一些操作(转)

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

随机推荐

  1. MySQL5.6复制技术(2)-主从部署以及半同步配置详细过程

    当前环境规划 主机名称 ec2t-pgtest-01 ec2t-pgtest-02 IP地址 10.189.102.118 10.189.100.195 角色 master slave 系统版本 Ce ...

  2. ORACLE PACKAGE中查看包的依赖关系

    SELECT dd.* FROM dba_dependencies dd WHERE NAME <> referenced_name AND referenced_type <> ...

  3. centos命令行系列之centos查看磁盘空间大小

    df -h 扩展: 1.查看当前文件夹所有文件大小 du -sh 2.查看指定文件下所有文件大小 du -h /data/ 3.查看指定文件大小 du -h install.log 4.查指定文件夹大 ...

  4. [LightOJ 1027] A Dangerous Maze

    A Dangerous Maze You are in a maze; seeing n doors in front of you in beginning. You can choose any ...

  5. F - Proud Merchants

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  6. [LeetCode] 20. Valid Parentheses ☆

    转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...

  7. zookeeper 的心跳

    假定:主机 A, B 通过 tcp 连接发送数据,如果拔掉 A 主机的网线,B 是无法感知到的.但是如果 A 定时给 B 发送心跳,则能根据心跳的回复来判断连接的状态. 以 zookeeper 为例: ...

  8. ActiveMQ topic 普通订阅和持久订阅

    直观的结果:当生产者向 topic 发送消息, 1. 若不存在持久订阅者和在线的普通订阅者,这个消息不会保存,当普通订阅者上线后,它是收不到消息的. 2. 若存在离线的持久订阅者,broker 会为该 ...

  9. VisualSVN+TortoiseSVN搭建版本控制系统教程

    Tortoise VisualSVN用作SVN的服务端,TortoiseSVN用作SVN的客户端. 一.安装和配置VisualSVN 1.1安装VisualSVN 下载链接:https://www.v ...

  10. python(4)之字典

    字典的操作方式如下: info={ 'stu1101':"xiaohai", 'stu1102':'liming', 'stu1103':"heima", } ...