-
- /*
- Model
- 对数据进行操作
-
- */
- //数据库相关配置
- define('HOST','服务器');
- define('USER','账号');
- define('PASS','密码');
- define('DBNAME','数据库名');
- define('CHARSET','字符集');
- class Model
- {
- //成员属性
- public $link;//数据库链接标识
- public $tableName;//表名
- public $pk;//主键
- public $fields;//字段
- public $where;//where条件
- public $limit;//limit条件
- public $order;//order条件
- public $field;//字段条件
- //构造方法
- public function __construct($tableName)
- {
- //初始化
- $this->tableName = $tableName;
- //链接数据库
- $this->link = mysqli_connect(HOST,USER,PASS) or die('数据库链接失败');
- // 选择库
- mysqli_select_db($this->link,DBNAME) or die('选择数据库失败');
- //设置字符集
- mysqli_set_charset($this->link,CHARSET) or die('设置字符集失败');
- //调用方法 获取表中字段
- $this->getTableFields();
- }
- //添加
- /*
- 参数:
- 关联数组
- 返回值:
- 成功 返回 刚插入的数据id
- 失败 false
- username=>'zhangsan',password =>'zhangsans'
-
- insert into user(username,password) values('zhangsan','zhangsans');
- */
- public function add(array $arr)
- {
- //检测是否有非法字段
- foreach ($arr as $k => $value) {
- if(!in_array($k,$this->fields)){
- //不存在
- die('您的参数中包含非法字段,请配合安检'.$k);
- }
- }
-
- //提取字段
- $keys = array_keys($arr);
- $k_sql = implode(',',$keys);
-
- //提取值
- $values = array_values($arr);
- $v_sql = '"'.implode('","',$values).'"';
-
- //拼接sql
- $sql = 'insert into '.$this->tableName.'('.$k_sql.') values('.$v_sql.')';
- //执行sql
- $res = mysqli_query($this->link,$sql);
- //判断 提取结果
- if($res){
- //返回最后插入的id
- return mysqli_insert_id($this->link);
- }else{
- return false;
- }
- }
- /*
- 修改
- 返回值 受影响行数
- [username="tainqi",password="tianji",'id'=>10]
- update user set username="tainqi",password="tianji" where id = ?
- */
- public function update(array $arr)
- {
- //检测是否有非法字段
- foreach ($arr as $k => $value) {
- if(!in_array($k,$this->fields)){
- if($k != $this->pk){
- //不存在
- die('您的参数中包含非法字段,请配合安检'.$k);
- }
- }
- }
- //拼接sql
- $kvsql='';
- $where='';
- //遍历
- foreach ($arr as $k => $v) {
- //检测是否为主键
- if($k == $this->pk){
- $where = ' where '.$k.' = '.$v;
- }else{
- $kvsql .= $k.'="'.$v.'",';
- }
- }
- //去掉最后的,
- $kvsql = rtrim($kvsql,',');
- $sql = 'update '.$this->tableName.' set '.$kvsql.$where;
- //执行sql
- $res = mysqli_query($this->link,$sql);
- if($res){
- return mysqli_affected_rows($this->link);
- }else{
- return false;
- }
- }
- //删除 where id = ?
- public function delete($id)
- {
- //拼接sql
- $sql = 'delete from '.$this->tableName.' where '.$this->pk.' = '.$id;
- //执行
- $res = mysqli_query($this->link,$sql);
- //判断
- if($res){
- return mysqli_affected_rows($this->link);
- }else{
- return false;
- }
- }
- //查询
- //单条查询
- public function find($id)
- {
- //sql
- $sql = 'select * from '.$this->tableName.' where '.$this->pk.' = '.$id;
- var_dump($sql);die;
- //执行
- $res = mysqli_query($this->link,$sql);
- //判断
- if($res){
- return mysqli_fetch_assoc($res);
- }else{
- return false;
- }
- }
- //多条查询
- public function select()
- {
- //检测是否存在where条件
- $w = '';
- if(!empty($this->where)){
- $w = ' where '.$this->where;
- }
- //检测是否存在limit条件
- $l = '';
- if(!empty($this->limit)){
- $l = ' limit '.$this->limit;
- }
- //检测是否存在 field 条件
- $f = '*';
- if(!empty($this->field)){
- $f = $this->field;
- }
- //检测是否存在 order 条件
- $o = '';
- if(!empty($this->order)){
- $o = ' order by '.$this->order;
- }
- //sql
- $sql = 'select '.$f.' from '.$this->tableName.$w.$o.$l;
- // echo $sql;
- //清空所有指令
- $this->where = '';
- $this->limit = '';
- $this->order = '';
- $this->field = '';
- //执行
- $result = mysqli_query($this->link,$sql);
- //判断
- if($result){
- $res = [];
- while($row = mysqli_fetch_assoc($result)){
- $res[] = $row;
- }
- return $res;
- }else{
- return false;
- }
- }
- //where 条件
- public function where($w)
- {
- //将条件存入属性中
- $this->where = $w;
- //返回对象
- return $this;
- }
- //limit 条件
- public function limit($l)
- {
- //将条件存入属性中
- $this->limit = $l;
- //返回对象
- return $this;
- }
- //field 条件
- public function field($f)
- {
- //将条件存入属性中
- $this->field = $f;
- //返回对象
- return $this;
- }
- //order 条件
- public function order($o)
- {
- //将条件存入属性中
- $this->order = $o;
- //返回对象
- return $this;
- }
- //统计总数
- public function count()
- {
- //检测是否存在where条件
- $w = '';
- if(!empty($this->where)){
- $w = ' where '.$this->where;
- }
- //sql
- $sql = 'select count(*) as total from '.$this->tableName.$w;
- //执行
- $res = mysqli_query($this->link,$sql);
- if($res){
- $res = mysqli_fetch_assoc($res);
- return $res['total'];
- }else{
- return false;
- }
- }
- //获取当前表中的所有字段信息 desc user
- public function getTableFields()
- {
- //拼接sql
- $sql = 'desc '.$this->tableName;
- //执行sql
- $res = mysqli_query($this->link,$sql);
- //声明变量和数组
- $pk = '';//主键
- $arr = [];//普通字段
- //判断 提取结果
- if($res){
- while($row = mysqli_fetch_assoc($res))
- {
- //判断是否为主键
- if($row['Key'] == 'PRI'){
- $pk = $row['Field'];
- }else{
- $arr[] = $row['Field'];
- }
- }
- //将字段和主键存入属性中
- $this->pk = $pk;
- $this->fields = $arr;
- }else{
- return false;
- }
- }
- //析构方法
- public function __destruct()
- {
- //关闭数据库
- mysqli_close($this->link);
- }
- }
- //实例化对象
- $user = new Model('user');
- ?>
复制代码
欢迎交流QQ:2326308699
0
|
|
|
|