zoukankan      html  css  js  c++  java
  • 增删改查类

    <?php
    // 所有数据表的基类
    abstract class Model {
    	protected $tableName = "";
    	protected $pdo = "";
    	protected $sql="";
    	function __construct() {
    		$pdo = new PDO( "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USERNAME, DB_PASSWORD );
    		$pdo->exec ( "set names " . DB_CHARSET );
    		$this->pdo = $pdo;
    	}
    	function updatetone($where=''){
    		$sql = 'UPDATE ' . $this->tableName . ' SET clicknum=clicknum+1'. ($where == NULL ? $where : ' WHERE ' . $where);
    		$this->sql=$sql;
    		return $this->pdo->exec ( $sql );
    	}
    	/**
    	 * 获取总记录数 select count(*) as num from news where cateid=5
    	 */
    	function count($where=""){
    		$where = empty($where) ? "" : " where ".$where;
    		$sql="select count(*) as num from {$this->tableName} $where";
    		$this->sql=$sql;
    		$pdoS=$this->pdo->query($sql);
    		$arr=$pdoS->fetch(PDO::FETCH_ASSOC);
    		return $arr['num'];
    	}
    	/**
    	 * 增加
    	 * array $arr=array('字段名'=>值,....) exp:array('username'=>'xiaowang','password'=>'123456')
    	 */
    	function insert($arr) {
    		// 把数组转化成sql语句
    		// sql语句是php向mysql,告诉她给我写一条记录进表
    		// insert into 表名(字段列表) value(数据列表)
    		// insert into adminuser(username,password) value('xiaowang','123456')
    		$fiedList = "";
    		$valueList = "";
    		foreach ( $arr as $k => $v ) {
    			$fiedList .= "," . $k;
    			$valueList .= ",'" . $v . "'";
    		}
    		$fiedList = substr ( $fiedList, 1 );
    		$valueList = substr ( $valueList, 1 );
    		// 拼sql
    		$sql = "insert into {$this->tableName}({$fiedList}) value({$valueList})";
    		$this->sql=$sql;
    		// 执行
    		$re = $this->pdo->exec ( $sql );
    		if ($re) {
    			// 返回主键id
    			return $this->pdo->lastInsertId ();
    		} else {
    			return false;
    		}
    	}
    	// 查询
    	function findByPk($id) {
    		// 获取表的主键名称 desc 表名
    		$pdoS = $this->pdo->query ( "desc " . $this->tableName );
    		$arr = $pdoS->fetchAll ( PDO::FETCH_ASSOC );
    		foreach ( $arr as $v ) {
    			if ($v ['Key'] == 'PRI') {
    				$fieldName = $v ['Field'];
    				break;
    			}
    		}
    		$sql = "select * from {$this->tableName} where $fieldName=$id";
    		$this->sql=$sql;
    		$pdoS = $this->pdo->query ( $sql );
    		return $pdoS->fetch ( PDO::FETCH_ASSOC );
    	}
    	/**
    	 * 查询多条记录
             * array $arr 用来管理查询语句的子句
    	 * array('limit'=>'0,5','where'=>"id=5")
    	 */
    	function findAll($arr = array()) {
    		// select 字段列表 from 表名
    		// where 条件 group by 字段 having 条件 order by 字段 desc|asc limit start,length
    		// select 字段列表 from 表1 as t1 join 表2 as t2 on t1.字段=t2.字段 join 表3 as t3 on t2.字段=t3.字段
    		// 拼sql语句
    		$field = isset ( $arr ['field'] ) ? $arr ['field'] : "*";
    		$where = isset ( $arr ['where'] ) ? " where " . $arr ['where'] : '';
    		$group = isset ( $arr ['group'] ) ? " group by " . $arr ['group'] : '';
    		$having = isset ( $arr ['having'] ) ? " having " . $arr ['having'] : "";
    		$order = isset ( $arr ['order'] ) ? " order by " . $arr ['order'] : '';
    		$limit = isset ( $arr ['limit'] ) ? " limit " . $arr ['limit'] : '';
    		$alias = isset ( $arr ['alias'] ) ? " as " . $arr ['alias'] : "";
    		$join = isset ( $arr ['join'] ) ? " join " . $arr ['join'] : '';
    		$sql = "select $field from {$this->tableName} $alias $join $where $group $having $order $limit";
    		$this->sql=$sql;
    		$pdoS = $this->pdo->query ( $sql );
    		if (is_object ( $pdoS )) {
    			return $pdoS->fetchAll ( PDO::FETCH_ASSOC );
    		} else {
    			return array ();
    		}
    	}
    	function delete($where = NULL) {
    		$sql = 'DELETE FROM ' . $this->tableName . ($where == NULL ? $where : ' WHERE ' . $where);
    		$this->sql=$sql;
    		return $this->pdo->exec ( $sql );
    	}
    	function update($bind, $where = NULL) {
    		$set = NULL;
    		foreach ( $bind as $field => $value ) {
    			if (is_null ( $set )) {
    				$set .= $field . "='" . $value . "'";
    			} else {
    				$set .= ',' . $field . "='" . $value . "'";
    			}
    		}
    		$sql = 'UPDATE ' . $this->tableName . ' SET ' . $set . ($where == NULL ? $where : ' WHERE ' . $where);
    		$this->sql=$sql;
    		return $this->pdo->exec ( $sql );
    	}
    	function query($sql){
    		//判断是更新语句,还是查询语句
    		if(preg_match("/^select/i",$sql)){
    			$pdoS=$this->pdo->query($sql);
    			return $pdoS->fetchAll(PDO::FETCH_ASSOC);
    		}else{
    			return $this->pdo->exec($sql);
    		}
    	}
    	function getLastSql(){
    		return $this->sql;
    	}
    }
    

      

  • 相关阅读:
    P1613 跑路
    数据挖掘-聚类分析(Python实现K-Means算法)
    使用scikit-learn 估计器分类
    数据挖掘-集成学习
    数据挖掘-关联分析 Apriori算法和FP-growth 算法
    scikit-learn 中常用的评估模型
    数据挖掘---支持向量机(SVM)
    数据挖掘-KNN-K最近邻算法
    数据挖掘-决策树
    数据挖掘-逻辑Logistic回归
  • 原文地址:https://www.cnblogs.com/zst062102/p/6089473.html
Copyright © 2011-2022 走看看