服务器之家

服务器之家 > 正文

php数据库操作model类(使用__call方法)

时间:2021-03-26 17:47     来源/作者:shyhero

本文实例讲述了php数据库操作model类。分享给大家供大家参考,具体如下:

该数据库操作类使用__call()方法实现了数据的查找功能。

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/*
作者 : shyhero
*/
define("HOSTNAME","127.0.0.1");
define("USERNAME","root");
define("PASSWORD","");
define("DATANAME","class");
class Model{
    private $link;
    private $tableName;
    private $zd;
    private $method = array(
      "where" => "",
      "order" => "",
      "limit" => "",
      "group" => "",
      "having" => ""
      );
    public function __construct($tableName){
      $this -> tableName = $tableName;
      try{
        $this -> link = mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATANAME);
        mysqli_set_charset($this -> link,"UTF8");
      }catch(Exception $e){
        echo "数据库连接失败";
      }
      $this -> desc();
    }
    public function __destruct(){
      mysqli_close($this -> link);
    }
    public function desc(){
      $sql = " desc {$this -> tableName}; ";
      $res = mysqli_query($this -> link,$sql);
      $arr = mysqli_fetch_all($res,MYSQLI_ASSOC);
      for($i = 0 ;$i < count($arr);$i++){
        $brr[] = $arr[$i]['Field'];
      }
      $this -> zd = $brr;
      return $brr;
    }
    public function __call($name,$value){
      $name = strtolower($name);
      if(array_key_exists($name,$this -> method)){
        if($name == 'order'){
          $this -> method['order'] = " order by ".$value[0];
        }elseif($name == 'group'){
        $this -> method['group'] = " group by ".$value[0];
        }else{
          $this -> method[$name] = " {$name} ".$value[0];
        }
      }else{
        return "the method is not found!";
      }
      return $this;
    }
    public function method(){
      return " {$this -> method['where']} {$this -> method['order']} {$this -> method['limit']} {$this -> method['group']} {$this -> method['having']}; ";
    }
    public function find($a="*"){
      if(in_array("{$a}",$this -> zd) || $a == "*"){
        $sql = "select {$a} from {$this -> tableName} {$this -> method()} ";
      }else{
        $sql = "select * from {$this -> tableName}";
      }
      //return $sql;
      $res = mysqli_query($this -> link,$sql);
      $arr = mysqli_fetch_all($res,MYSQLI_ASSOC);
      return $arr;
    }
}

用法示例:

?
1
2
3
4
5
6
7
<?php
  function __autoload($className){
    require($className.".class.php");
  }
  $a = new Model("stu");
  $a -> where("name = 'zhu'")->limit("5,10");
  var_dump($a -> find("name"));

希望本文所述对大家PHP程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部