赞
踩
简单的用mysqli连接数据库实现的链式增删改查的类,大家可以自行扩展多表查询方法,关联查询方法等。对于用惯了thinkphp的同学来说,这个轻量级的类对于开发微型应用应该对您有所帮助。
- <?php
- define("HOST","host");
- define("USER", "user");
- define("PASSWORD", "pwd");
- define("DBNAME", "dbname");
- class Model{
- public $field = '*';
- public $tabname;
- public $where;
- public $order;
- public $limit;
- private $connect;
- //构造函数,链接数据库,给表赋值
- public function __construct($tabname){
- $this->connect = mysqli_connect(HOST,USER,PASSWORD,DBNAME);
- if (!$this->connect) die("连接错误: " . mysqli_connect_error());
- $this->tabname=$tabname;
- }
- //组合字段
- public function field($field){
- $this->field=$field;
- return $this;
- }
- //组合where条件
- public function where($where){
- $this->where="where ".$where;
- return $this;
- }
- //组合order排序条件
- public function order($order){
- $this->order="order by ".$order;
- return $this;
- }
- //组合limit限制条数
- public function limit($limit){
- $this->limit="limit ".$limit;
- return $this;
- }
- //组合和执行select语句
- public function select(){
- $sql="select {$this->field} from {$this->tabname} {$this->where} {$this->order} {$this->limit}";
- $rst=mysqli_query($this->connect,$sql);
- if(!$rst) $this->error();
- while ($row=mysqli_fetch_assoc($rst)) {
- $rows[]=$row;
- }
- return $rows;
- }
-
- //组合和执行insert语句单条插入
- public function insert($post){ //数据库插入操作,接收数组
- foreach ($post as $key => $value) {
- $keys[]=$key;
- $vals[]="'".$value."'";
- }
- $keyStr=join(",",$keys);
- $valStr=join(",",$vals);
- $sql="insert into {$this->tabname} ($keyStr) values ($valStr)";
- $rst = mysqli_query($this->connect,$sql);
- if ($rst) {
- return mysqli_insert_id($this->connect);
- }else{
- $this->error();
- }
- }
-
-
- //组合和执行insert语句多条插入
- /*
- $post['keys'] = ['id','name','old']
- $post['values'] = [
- [1,'张三','29'],
- [2,'李四','39']
- ]
- */
- public function inserts($post){ //数据库插入操作,接收二维数组
- $keyStr = join(",",$post['keys']);
- $valStr = '';
- foreach ($post['values'] as $value) {
- $vals = [];
- foreach ($value as $v){
- $vals[] = "'".$v."'";
- }
- $valStr .= '('.join(',',$vals).'),';
- }
- $valStr = trim($valStr,',');
- $sql="insert into {$this->tabname} ($keyStr) values $valStr";
- $rst = mysqli_query($this->connect,$sql);
- if ($rst) {
- return mysqli_affected_rows($this->connect);
- }else{
- $this->error();
- }
- }
- //删除数据
- public function delete(){
- if(!$this->where) die('删除数据必须指定删除条件,否则会删除数据库所有数据,请谨慎!');
- $sql="delete from {$this->tabname} {$this->where}";
- $rst = mysqli_query($this->connect,$sql);
- if ($rst) {
- return mysqli_affected_rows($this->connect);
- }else{
- $this->error();
- }
- }
- //组合和执行updata语句
- public function update($post){
- foreach ($post as $key => $value) {
- $sets[]="{$key}='{$value}'";
- }
- $setStr=join(",",$sets);
- $sql="update {$this->tabname} set {$setStr} {$this->where} ";
- $rst = mysqli_query($this->connect,$sql);
- if ($rst) {
- return mysqli_affected_rows($this->connect);
- }else{
- $this->error();
- }
- }
- //从表中取一行数据
- public function find(){
- $sql="select {$this->field} from {$this->tabname} {$this->where} limit 1";
- $rst=mysqli_query($this->connect,$sql);
- if(!$rst) $this->error();
- while ($row=mysqli_fetch_assoc($rst)) {
- $rows=$row;
- }
- return $rows;
- }
-
- //错误信息
- private function error(){
- die(mysqli_error($this->connect));
- }
-
- //获取总行数
- public function total(){
- $sql="select count(*) from {$this->tabname}";
- $rst=mysqli_query($this->connect,$sql);
- if ($rst) {
- $row=mysqli_fetch_row($rst);
- return $row[0];
- }else{
- $this->error();
- }
- }
- }
- //Model类对象工厂
- function M($tabname){
- return new Model($tabname);
- }
使用方法
- <?php
- include 'M.class.php';
- $data = [
- 'follow_group_id' => 1,
- 'follow_id' => 10,
- 'fid' => 20,
- 'uid' => 30
- ];
- $datas = [
- 'keys' => ['follow_group_id','follow_id','fid','uid'],
- 'values'=> [
- ['1',3,4,5],
- [6,7,8,9]
- ]
- ];
- //插入单条数据
- $insert = M('数据表名')->insert($data);
- //插入多条
- $inserts= M('数据表名')->inserts($datas);
- //查询多条数据
- $datas = M('数据表名')->field('id,name,old')->where('id>10')->order('id desc')->limit('0,50')->select();
- //查询单条数据
- $data = M('数据表名')->field('id,name,old')->where('id>10')->order('id desc')->limit('0,50')->find();
- //删除数据
- $del = M('数据表名')->where('id=10')->delete();
- //修改数据
- $update = M('数据表名')->where('id=10')->update($data);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。