<?php/*****************************model类(使用mysql扩展访问数据库)******************************/class model{ private $table; //数据库链接 private $link; //最后一次执行的sql语句 private $sql; private $tablestring; private $fieldstring; private $wherestring; private $orderstring; private $limitstring; //初始化类 public function __construct($tablename){ $dbinfo=array( 'hostname'=>'192.168.1.246', 'username'=>'liuchuan', 'password'=>'liuchuan123', 'database'=>'whatcart' ); $this->link=new mysqli($dbinfo['hostname'],$dbinfo['username'],$dbinfo['password'],$dbinfo['database']); if($this->link->connect_errno) trigger_error('error:could not make a database link ('.$this->link->connect_errno.')'.$this->link->connect_errno); $this->link->set_charset("utf8"); $this->fieldstring='*'; $this->tablestring=$tablename; $this->leftjoinstring=''; $this->wherestring=''; $this->orderstring=''; $this->limitstring=''; } /********封装的mysqli扩展方法********/ //参数过滤,输入参数只能是基本类型或一维数组 public function escape($param){ if(is_array($param)){ foreach ($param as $key => $value) { $param[$key]=$this->link->real_escape_string($value); } }else{ $param=$this->link->real_escape_string($value); } return $param; } //获取插入后生成的id public function getlastid(){ return $this->link->insert_id; } //获取影响的行数 public function countaffected(){ return $this->link->affected_rows; } //执行sql命令返回true或false public function execute($sql){ $this->sql=$sql; $result=$this->link->real_query($this->sql); if(false===$result){ trigger_error('<br/>error message:'.$this->link->error.'<br/>the sql:'.$this->sql); return false; }else{ $sql=strtolower($this->sql); return (false===strpos($sql,'insert')?true:$this->link->insert_id;//如果包含insert就返回插入记录的id } } //执行sql查询返回关联数组 public function query($sql){ $result=$this->link->real_query($sql); $this->sql=$sql; if($result!==false){ $record=array(); $list=array(); while($record=$result->fetch_assoc()){ $list[]=$record; } return $list; }else{ trigger_error('<br/>error message:'.$this->link->error.'<br/>the sql:'.$this->sql); return false; } } /********单表增删查改********/ public function create($table,$model){ $fields=''; $values=''; foreach ($model as $key => $value) { if($fields){ $fields.=','; } $fields.="`$key`"; if($values){ $values.=','; } $values.=is_numeric($value)?$value:"'".$this->link->real_escape_string($value)."'"; } $this->sql="insert into `$table`($fields) values($values)"; if(false===$this->link->real_query($this->sql)) trigger_error('<br/>error message:'.$this->link->error.'<br/>the sql:'.$this->sql); return $this->link->insert_id; } public function modify($table,$model,$where){ $assins=''; $where=$this->rewhere($where); foreach ($model as $key => $value) { $rkey=$this->link->real_escape_string($key); $rvalue=$this->link->real_escape_string($value); if(!is_numeric($rvalue)){ $rvalue="'".$rvalue."'"; } $assins.=$assins?",`$rkey`=$rvalue":"`$rkey`=$rvalue"; } $result=$this->link->real_query($this->sql); if(false===$result) trigger_error('<br/>error message:'.$this->link->error.'<br/>the sql:'.$this->sql); } public function remove($table,$where){ $where=$this->rewhere($where); $this->sql="delete from `$table` where $where"; $result=$this->link->real_query($this->sql); if(false===$result) trigger_error('<br/>error message:'.$this->link->error.'<br/>the sql:'.$this->sql); return $flag; } public function unique($where){ $where=$this->rewhere($where); $this->sql="select * from `$table` where $where limit 1"; $result=$this->link->real_query($this->sql); if($result===false) trigger_error('<br/>error message:'.$this->link->error.'<br/>the sql:'.$this->sql); return $result -> fetch_object(); } public function countrow($where){ $where=$this->rewhere($where); $this->sql=($where=='')?"select count(*) as 'totalrow' from `{$this->tablestring}`":"select count(*) from `{$this->tablestring}` where $where"; $result=$this->link->real_query($this->sql); if($result===false) trigger_error('<br/>error message:'.$this->link->error.'<br/>the sql:'.$this->sql); $record=$result->fetch_object(); return $record->totalrow; } //按条件查找函数 public function search($table,$fields,$leftjoin='',$where='',$order='',$limit=''){ if(is_array($fields)) $fields=implode(",",$fields); $this->sql=empty($fields)?"select * from `$table`":"select $fields from `$table`"; if(!empty($where)){ $where=$this->rewhere($where); $this->sql.=" where $where"; } if(!empty($order)) $this->sql.=" order by $order"; if(!empty($order)) $this->sql.="limit $limit"; $result=$this->link->real_query($this->sql); if($result===false){ trigger_error('<br/>error message:'.$this->link->error.'<br/>the sql:'.$this->sql); }else{ $tempitem=array(); $templist=array(); while($tempitem=$result -> fetch_assoc()){ if(!empty($tempitem['create_time'])) $tempitem['create_time_exp']=date('y-m-d h:i:s',$tempitem['create_time']); if(!empty($tempitem['add_time'])) $tempitem['add_time_exp']=date('y-m-d h:i:s',$tempitem['end_time']); if(!empty($tempitem['start_time'])) $tempitem['start_time_exp']=date('y-m-d h:i:s',$tempitem['start_time']); if(!empty($tempitem['end_time'])) $tempitem['end_time_exp']=date('y-m-d h:i:s',$tempitem['end_time']); $templist[]=$tempitem; }; return $templist; } } public function keyin($ids){ if(!empty($ids)){ return false; } if(is_array($ids)){ foreach ($ids as $key => $value) { $ids[$key]=$this->link->real_escape_string($value); } $ids=implode(',',$ids); }else{ $ids=$this->link->real_escape_string($ids); } $primary=$this->getprimarykey(); if(!empty($this->wherestring)) $this->wherestring.=" and "; $this->wherestring.="`$primary` in ($ids)"; return $this; }/******************仿thinkphp的链式操作*******************************/ //设置查询的字段 public function field($field){ if(is_array($field)) $field=implode(',',$field); $this->fieldstring=$field; return $this; } public function table($table){ $this->tablestring=$table; } public function order($order){ $this->orderstring=$order; } public function limit($limit){ $this->limitstring=$limit; } //将数组格式的条件组装成字符串 public function where($where){ if(is_array($where)&&count($where)>0){ $str=''; foreach($where as $key=>$value){ if(!empty($str)){ $str.=' and '; } $rekey=$this->link->real_escape_string($key); $revalue=$this->link->real_escape_string($value); $str.=is_numeric($revalue)?"`$rekey`=$revalue":"`$rekey`='$revalue'"; } $this->wherestring=$str; }else{ $this->wherestring=$where; } return $this; } //删除 public function delete(){ if(empty($this->wherestring)) trigger_error('<br/>error message:删除条件不可以是空'); $this->sql="delete from {$this->tablestring} where {$this->wherestring}"; } //更新 public function update($model){ if(empty($this->wherestring)) trigger_error('<br/>error message:更新条件不可以是空'); if(empty($model)||count($model)==0) trigger_error('<br/>error message:更新参数不可以是空') $assins=''; foreach ($model as $key => $value) { $rkey=$this->link->real_escape_string($key); $rvalue=$this->link->real_escape_string($value); if(!is_numeric($rvalue)){ $rvalue="'".$rvalue."'"; } $assins.=$assins?",`$rkey`=$rvalue":"`$rkey`=$rvalue"; } $this->sql="update {$this->tablestring} set $assins where {$this->wherestring}"; } //查询 public function select(){ $this->sql="select {$this->fieldstring} from {$this->tablestring}"; if(!empty($this->wherestring)) $this->sql.=' where '.$this->wherestring; if(!empty($this->orderstring)) $this->sql.=' order by '.$this->orderstring; if(!empty($this->limitstring)) $this->sql.=' limit '.$this->limitstring; return $this->query($this->$sql); } /*开发环境用于调试的语句,生产环境可以删除*/ public function getsql(){ return $this->sql; }}
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号