-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPdoMySQL.class.php
More file actions
197 lines (183 loc) · 5.29 KB
/
Copy pathPdoMySQL.class.php
File metadata and controls
197 lines (183 loc) · 5.29 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* pdo数据库操作类
*/
class PdoMySQL{
private $_config=array();
private $_pdo;
private $_pdoStatement;
private $_lastId;
function __construct($username,$passwd,$db,$options=array(),$dbType='mysql',$host='localhost',$charType='utf8') {
if(!class_exists('PDO')){
$this->showError('PDO没有开启,请开启后使用');
}
if(!empty($db) && !empty($username) && !empty($passwd)){
$this->_config=array(
'dbType'=>$dbType,
'host'=>$host,
'charset'=>$charType,
'username'=>$username,
'passwd'=>$passwd,
'dsn'=>$dbType.':host='.$host.';dbname='.$db,
'options'=>$options
);
}
try{
$this->_pdo=new PDO($this->_config['dsn'],$this->_config['username'],$this->_config['passwd'],$this->_config['options']);
}catch(PDOException $e){
$this->_showError($e->getMessage());
}
if(!$this->_pdo){
$this->_showError('PDO链接错误');
}
$this->_pdo->exec('set names '.$this->_config['charset']);
}
public function getAll($sql=''){
if(strlen($sql)>0){
if(!empty($this->_pdoStatement)){
$this->_free();
}
$this->_pdoStatement=$this->_pdo->query($sql);
if($this->_pdoStatement->rowCount()){
$result=$this->_pdoStatement->fetchAll(PDO::FETCH_ASSOC);
}else{
$this->_showError('没有数据集返回');
}
return $result;
}else{
$this->_showError('SQL语句有误,请检查');
}
}
public function get_one_row($sql=''){
if(strlen($sql)>0){
if(!empty($this->_pdoStatement)){
$this->_free();
}
$this->_pdoStatement=$this->_pdo->query($sql);
if($this->_pdoStatement->rowCount()){
$result=$this->_pdoStatement->fetch(PDO::FETCH_ASSOC);
}else{
$this->_showError('没有数据集返回');
}
return $result;
}else{
$this->_showError('SQL语句有误,请检查');
}
}
public function op_insert($data=array(),$dbName){
$keys=array_keys($data);
$values=array_values($data);
$values="'".implode("','", $values)."'";
if(count(array_diff_assoc($keys, range(0, sizeof($data))))!=0){//如果是关联数组
$keys=implode(',', $keys);
$sql="insert into {$dbName}({$keys}) values({$values})";
}else{
$sql="insert into {$dbName} values({$values})";
}
echo $sql;
if($this->_pdo->exec($sql)){
return true;
}else{
return false;
}
}
public function op_delete($table,$where=null,$group=null,$having=null,$order=null,$limit=null){
$sql='delete from '.$table
.$this->_parseWhere($where)
.$this->_parseGroup($group)
.$this->_parseHaving($having)
.$this->_parseOrder($order)
.$this->_parseLimit($limit);
return $this->_pdo->exec($sql);
}
public function op_update($table,$data=array(),$where=null,$group=null,$having=null,$order=null,$limit=null){
$sets='';
foreach ($data as $key => $value) {
$sets.=$key.'='.$value.',';
}
$sets=rtrim($sets,',');
$sql="update {$table} set {$sets} "
.$this->_parseWhere($where)
.$this->_parseGroup($group)
.$this->_parseHaving($having)
.$this->_parseOrder($order)
.$this->_parseLimit($limit);
return $this->_pdo->exec($sql);
}
public function getLastId(){
if(!isset($this->_pdo)){
return false;
}
return $this->_pdo->lastInsertId();
}
public function getNumOfTable($tables,$where=null,$group=null,$having=null,$order=null,$limit=null){
$sql="select count(*) from {$tables}"
.$this->_parseWhere($where)
.$this->_parseGroup($group)
.$this->_parseHaving($having)
.$this->_parseOrder($order)
.$this->_parseLimit($limit);
return $this->get_one_row($sql);
}
public function find($fields='*',$table,$where=null,$group=null,$having=null,$order=null,$limit=null){
$sql='select '.$fields.' from '.$table
.$this->_parseWhere($where)
.$this->_parseGroup($group)
.$this->_parseHaving($having)
.$this->_parseOrder($order)
.$this->_parseLimit($limit);
return $this->getAll($sql);
}
private function _parseWhere($where){
$whereStr='';
if(is_string($where) && !empty($where)){
$whereStr.=$where;
}
return empty($whereStr)?'':' where '.$whereStr;
}
private function _parseGroup($group){
$groupStr='';
if(is_string($group) && !empty($group)){
$groupStr.=$group;
}elseif (is_array($group)) {
$groupStr.=implode(',', $group);
}
return empty($groupStr)?'':'group by '.$groupStr;
}
private function _parseHaving($having){
$havingStr='';
if(is_string($having) && !empty($having)){
$havingStr.=$having;
}
return empty($havingStr)?'':'having '.$havingStr;
}
private function _parseOrder($order){
$orderStr='';
if(is_string($order)){
$orderStr.=$order;
}elseif (is_array($order)) {
$orderStr.=implode(',', $order);
}
return empty($orderStr)?'':' order by '.$orderStr;
}
private function _parseLimit($limit){
$limitStr='';
if(is_string($limit) && !empty($limit)){
$limitStr.='limit '.$limit;
}elseif (is_array($limit)) {
if(sizeof($limit)>1){
$limitStr.='limit '.$limit[0].','.$limit[1];
}else{
$limitStr.='limit '.$limit[0];
}
}
return empty($limitStr)?'':$limitStr;
}
private function _free(){
$this->_pdoStatement=null;
}
private function _showError($errorMsg=''){
$error='<script>alert({$errorMsg});</script>';
}
}
?>