-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFlamez57Mysqli.php
More file actions
146 lines (131 loc) · 3.44 KB
/
Flamez57Mysqli.php
File metadata and controls
146 lines (131 loc) · 3.44 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
define("HOSTNAME","localhost");
define("USERNAME","root");
define("PASSWORD","");
define("DATANAME","");
define("PORT","3306");
define("CHARSET","UTF8");
/*
* Flamez57Mysqli使用说明:
* ===========================================================
* 实例化连接器
* $mysql = new Flamez57Mysqli(tablename);
* ===========================================================
* 获取当前表字段
* $mysql->fields();
* -----------------------------------------------------------
* 条件查询
* $mysql
* ->where()
* ->order(field,[desc])
* ->group(field)
* ->having()
* ->limit(size,[start])
* ->find(field);
*
*
*/
class Flamez57Mysqli
{
//数据库连接
private $dbLink;
//要操作的数据表
private $tableName;
//当前表字段
private $fields;
private $method = [
"where" => "",
"order" => "",
"limit" => "",
"group" => "",
"having" => ""
];
/*
* 连接数据库
*/
public function __construct($tableName)
{
$this->tableName = $tableName;
try{
$this->dbLink = mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATANAME,PORT);
mysqli_set_charset($this->dbLink,CHARSET);
}catch(Exception $e){
$this->errorMsg("数据库连接失败".mysqli_connect_error());
}
$this->fields();
}
/*
* 释放数据库连接
*/
public function __destruct()
{
mysqli_close($this->dbLink);
}
/*
* 返回错误信息
*/
private function errorMsg($msg)
{
echo "Flamez57Bug:".$msg;
}
/*
* 查看表字段
*/
public function fields()
{
$sql = " DESC {$this -> tableName}; ";
$res = mysqli_query($this -> dbLink,$sql);
$datas = mysqli_fetch_all($res,MYSQLI_ASSOC);
for($i = 0 ;$i < count($datas);$i++){
$fields[] = $datas[$i]['Field'];
}
$this->fields = $fields;
return $fields;
}
/*
* 自动查找调用的方法执行后返回对象本身
*/
public function __call($name,$value)
{
$name = strtolower($name);
if(array_key_exists($name,$this -> method)){
switch ($name) {
case 'order':
$this->method['order'] = "ORDER BY ".$value[0]." ".(isset($value[1]) ? $value[1] : "ASC");
break;
case 'group':
$this->method['group'] = "GROUP BY ".$value[0];
break;
case 'limit':
$this->method['limit'] = "LIMIT ".(isset($value[1]) ? $value[1]."," : "").$value[0];
break;
default:
$this->method[$name] = $name." ".$value[0];
}
}else{
$this->errorMsg("the method is not found!");
}
return $this;
}
/*
* 将被掉到的方法拼接
*/
public function method()
{
return " {$this -> method['where']} {$this -> method['group']} {$this -> method['having']} {$this -> method['order']} {$this -> method['limit']} ; ";
}
/*
* 条件查询
*/
public function find($field = "*")
{
if (in_array($field, $this->fields) || $field == '*') {
$sql = "SELECT {$field} FROM {$this->tableName} {$this->method()} ";
} else {
$sql = "SELECT * FROM {$this->tableName}";
}
$res = mysqli_query($this->dbLink,$sql);
$arr = mysqli_fetch_all($res,MYSQLI_ASSOC);
return $arr;
}
}