企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
> 请在PHP>=5.6版本使用 ## 类库 支持方法 > 类库支持链式操作,和thinkphp Db操作类类似 ## 使用方法 1. 第一步在入口文件引入数据配置信息 2. Db::name('指定表')切入指定表 ## where方法解读(where,whereIn,whereLike) > 支持 在where里直接写入 mysql where条件,支持数组查询比如['id'=>1] > whereIn ('字段','in的内容') 支持 数组和字符串 > whereLike('字段','%like内容%') ## 可支持操作 > field order limit group update insert delete count和thinkphp使用一致 ## 数据库分页方法(paginate) > 参数 1 每页显示数量 2接收请求的page字段 ## 代码引入 Db.php ```php /** * Class Db * @package Db * @author Mr.taochuang <mr_taochuang@163.com> * @date 2022/6/1 11:35 * 数据库类 * * @method \taochuang\DbQuery name($table) static 操作表 * */ class Db { private static $connect = [ 'hostname' => '172.17.0.7', // 数据库名 'database' => 'u1001349', // 用户名 'username' => 'sql_sfzd', // 密码 'password' => '!@#sfsdfsd739u^', // 端口 'hostport' => '3306', //字符集 'charset' => 'utf8mb4' ]; public static function __callstatic($name, $arguments) { $connect = self::$connect; //链接数据库并且加载DbQuery类 执行数据库操作 return (new \ReflectionClass(DbQuery::class))->newInstanceArgs([new \PDO("mysql:host={$connect['hostname']};dbname={$connect['database']};charset={$connect['charset']}", $connect['username'], $connect['password'])])->$name($arguments[0]); } } ``` DbQuery.php ```php /** * Class DbQuery * @package DbQuery * @author Mr.taochuang <mr_taochuang@163.com> * @date 2022/6/1 11:35 * 数据库执行类 * * @method \taochuang\DbQuery name($table) table * @method \taochuang\DbQuery limit($limit) limit * @method \taochuang\DbQuery field($where) field * @method \taochuang\DbQuery where($where) where * @method \taochuang\DbQuery order($where) order * @method \taochuang\DbQuery group($where) group */ class DbQuery { /** * @var \PDO */ public $query; public $name = ''; public $field = ''; public $order= ''; public $group=''; public $where = []; public $limit = 0; public $page_num=15; public $page=1; public $is_empty = 1; public $error; public function __construct($query) { $this->query = $query; } public function queryWhere($where) { array_push($this->where, $where); return $this; } public function __call($name, $arguments) { $fnc = 'query' . ucwords($name); if (method_exists($this, $fnc)) $this->$fnc($arguments); $this->$name = $arguments[0]; return $this; } public function whereIn($field,$storm){ if(is_array($storm)) $storm=implode(',',$storm); $this->where[$field]=' in ('.$storm.')'; return $this; } public function whereLike($field,$storm){ $this->where[$field]=' like "'.$storm.'"'; return $this; } public function select() { $this->query = $this->query->prepare($this->querySql(__FUNCTION__)); return $this->fetch(); } public function insert($data){ if(empty($data)) exit('请输入数据'); $query=$this->query; $sql=$this->querySql(__FUNCTION__,$data); $this->query = $this->query->prepare($sql); $this->query->execute(); $this->error=$this->query->errorInfo(); return $query->lastInsertId(); } public function update($data){ if(empty($data)) exit('请输入数据'); $this->query = $this->query->prepare($this->querySql(__FUNCTION__,$data)); $res=$this->query->execute(); $this->error=$this->query->errorInfo(); return $res; } public function delete(){ $this->query = $this->query->prepare($this->querySql(__FUNCTION__)); $res=$this->query->execute(); $this->error=$this->query->errorInfo(); return $res; } public function find() { $data = $this->select(); return $this->is_empty == 1 ? $data[0] : []; } public function fetch() { $this->query->execute(); $data = $this->query->fetchAll(\PDO::FETCH_ASSOC); $this->is_empty = 1; if (empty($data[0])) $this->is_empty = 0; return $data; } public function paginate($page_num=15,$page='page'){ $page=$_POST[$page]; $this->page=empty($page)?1:$page; $this->page_num=$page_num; $query=$this->query; $this->query = $this->query->prepare($this->querySql(__FUNCTION__)); $data= $this->fetch(); $total=$this->count('id',$query); $last_page=(string)ceil($total/$page_num); return ['total'=>$total,'per_page'=>$page_num,'current_page'=>$this->page,'last_page'=>$last_page,'data'=>$data]; } public function count($count_field='id',$query=null){ $query=empty($query)?$this->query:$query; $query=$query->prepare($this->querySql('count',[],$count_field)); $query->execute(); $total=$query->fetch(\PDO::FETCH_ASSOC)['total']; return $total; } public function value($field){ $data=$this->find(); return empty($data[$field])?'':$data[$field]; } public function querySql($sql_type,$up_data=[],$count_field='id'){ list($where,$key,$value,$up,$paginate)=['',[],[],'','']; if (!empty($this->where) && is_array($this->where)) { $wh = ''; foreach ($this->where as $k => $v) { if(strpos($v,'like')!==false || strpos($v,'in')!==false){ $wh.=$k.$v; }else{ $wh .= $k . '="' . $v . '" and '; } } $wh = trim($wh, ' and '); $where = ' where ' . $wh; } if (!empty($this->where) && is_string($this->where)) $where = ' where ' . $this->where; $field = empty($this->field) ? '*' : $this->field; $order=empty($this->order)?'':' order by '.$this->order; $limit = empty($this->limit) ? '' : ' limit '.$this->limit; $group =empty($this->group) ?'':' group by '.$this->group; if(!empty($up_data)) { if($sql_type=='insert'){ foreach ($up_data as $k => $v) { array_push($key, $k); array_push($value, '"' . $v . '"'); } $key = implode(',', $key); $value = implode(',', $value); } if($sql_type=='update'){ foreach($up_data as $k=>$v){ $up.=$k.'="'.$v.'",'; } } } if($sql_type=='paginate'){ $paginate=' limit '.($this->page-1)*$this->page_num.','.$this->page_num; } if(empty($key)){ $key='';$value=''; } $up=trim($up,','); $sql_storm =[ 'select'=>'SELECT ' . $field . ' FROM ' . $this->name, 'insert'=>'INSERT '.'INTO '.$this->name.' ('.$key.') values ('.$value.')', 'update'=>'UPDATE '.$this->name.' SET '.$up, 'delete'=>'DELETE '.'FROM '.$this->name, 'paginate'=>'SELECT ' . $field . ' FROM ' . $this->name, 'count'=>'SELECT '.'COUNT('.$count_field.') total'.' FROM ' . $this->name, ]; $sql=$sql_storm[$sql_type]; if(!empty($where)) $sql.=$where; if(!empty($order)) $sql.=$order; if(!empty($limit) && $sql_type!='paginate') $sql.=$limit; if(!empty($group)) $sql.=$group; if($sql_type=='paginate') $sql.=$paginate; return $sql; } public function exec($sql){ return $this->query->prepare($sql)->execute(); } } ```