SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | class SQL | |
3 | - | class SQL { |
3 | + | { |
4 | - | protected $con; |
4 | + | protected $con; |
5 | - | private $server; |
5 | + | private $server; |
6 | - | private $host = 'localhost'; |
6 | + | private $host = 'localhost'; |
7 | - | private $user = 'root'; |
7 | + | private $user = 'root'; |
8 | - | private $pass = ''; |
8 | + | private $pass = ''; |
9 | - | private $debe = 'absen'; |
9 | + | private $debe = 'absen'; |
10 | - | public $success = null; |
10 | + | public $success = null; |
11 | - | public $message = null; |
11 | + | public $message = null; |
12 | - | var $attribute = array( |
12 | + | var $attribute = array( |
13 | - | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
13 | + | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
14 | - | PDO::ATTR_CASE => PDO::CASE_NATURAL, |
14 | + | PDO::ATTR_CASE => PDO::CASE_NATURAL, |
15 | - | ); |
15 | + | ); |
16 | ||
17 | - | function __construct($h = null, $u = null, $p = null, $d = null) { |
17 | + | function __construct($h = null, $u = null, $p = null, $d = null) |
18 | - | if($h == null) { |
18 | + | { |
19 | - | $this->server = 'mysql:host='.$this->host.';dbname='.$this->debe; |
19 | + | if ($h == null) |
20 | - | } else { |
20 | + | { |
21 | - | $this->server = 'mysql:host='.$h.';dbname='.$d; |
21 | + | $this->server = 'mysql:host=' . $this->host . ';dbname=' . $this->debe; |
22 | - | $this->user = $u; |
22 | + | } |
23 | - | $this->pass = $p; |
23 | + | else |
24 | - | } |
24 | + | { |
25 | $this->server = 'mysql:host=' . $h . ';dbname=' . $d; | |
26 | - | try { |
26 | + | $this->user = $u; |
27 | - | $this->con = new PDO($this->server, $this->user, $this->pass, $this->attribute); |
27 | + | $this->pass = $p; |
28 | - | $this->success = true; |
28 | + | } |
29 | - | } |
29 | + | |
30 | - | catch (PDOException $e) { |
30 | + | try |
31 | - | $this->success = false; |
31 | + | { |
32 | - | $this->message = $e->getMessage(); |
32 | + | $this->con = new PDO($this->server, $this->user, $this->pass, $this->attribute); |
33 | - | } |
33 | + | $this->success = true; |
34 | - | } |
34 | + | } |
35 | catch(PDOException $e) | |
36 | - | public function dbstatus() { |
36 | + | { |
37 | - | if($this->con == null) { |
37 | + | $this->success = false; |
38 | - | return $this->message; |
38 | + | $this->message = $e->getMessage(); |
39 | - | } |
39 | + | } |
40 | - | } |
40 | + | } |
41 | ||
42 | - | public function response() { |
42 | + | public function dbstatus() |
43 | - | if($this->success == false) { |
43 | + | { |
44 | - | return $this->message; |
44 | + | if ($this->con == null) |
45 | - | } else { |
45 | + | { |
46 | - | return 1; |
46 | + | return $this->message; |
47 | - | } |
47 | + | } |
48 | - | } |
48 | + | } |
49 | ||
50 | - | public function close() { |
50 | + | public function response() |
51 | - | $this->con = null; |
51 | + | { |
52 | - | } |
52 | + | if ($this->success == false) |
53 | { | |
54 | - | public function insertid() { |
54 | + | return $this->message; |
55 | - | return $this->con->lastInsertId(); |
55 | + | } |
56 | - | } |
56 | + | else |
57 | - | /* |
57 | + | { |
58 | - | if any string with `field` will be replace with `` |
58 | + | return 1; |
59 | - | */ |
59 | + | } |
60 | - | public function insertUPLOAD($table ,$data) |
60 | + | } |
61 | - | { |
61 | + | |
62 | - | try { |
62 | + | public function close() |
63 | - | $field = $value = ''; |
63 | + | { |
64 | - | foreach($data as $f=>$v) { |
64 | + | $this->con = null; |
65 | - | $field .= ',`'.str_replace(':','',str_replace("field","",$v)).'`'; |
65 | + | } |
66 | - | $value .= ','.$v; |
66 | + | |
67 | - | } |
67 | + | public function insertid() |
68 | - | $field = substr($field,1); |
68 | + | { |
69 | - | $value = substr($value,1); |
69 | + | return $this |
70 | - | $query = 'INSERT INTO '.$table.' ('.$field.') VALUES ('.$value.')'; |
70 | + | ->con |
71 | - | $st = $this->con->prepare($query); |
71 | + | ->lastInsertId(); |
72 | - | $st->execute($data); |
72 | + | } |
73 | - | $this->success = true; |
73 | + | /* |
74 | - | } |
74 | + | if any string with `field` will be replace with `` |
75 | - | catch (PDOException $e) { |
75 | + | */ |
76 | - | $this->success = false; |
76 | + | public function insertUPLOAD($table, $data) |
77 | - | $this->message = $e->getMessage(); |
77 | + | { |
78 | - | } |
78 | + | try |
79 | - | } |
79 | + | { |
80 | - | public function insert($table, $data) { |
80 | + | $fields = $values = array(); |
81 | - | try { |
81 | + | foreach ($data as $f => $v) |
82 | - | $field = $value = ''; |
82 | + | { |
83 | - | foreach($data as $f=>$v) { |
83 | + | $fields[]= '`' . str_replace(':', '', str_replace("field", "", $f)) . '`'; |
84 | - | $field .= ','.str_replace(':','',$v); |
84 | + | $values[]= "'" . addslashes($v)."'"; |
85 | - | $value .= ','.$v; |
85 | + | } |
86 | - | } |
86 | + | |
87 | - | $field = substr($field,1); |
87 | + | $field = implode(",", $fields ); |
88 | - | $value = substr($value,1); |
88 | + | $value = implode(",", $values ); |
89 | - | $query = 'INSERT INTO '.$table.' ('.$field.') VALUES ('.$value.')'; |
89 | + | $query = 'INSERT INTO ' . $table . ' (' . $field . ') VALUES (' . $value . ')'; |
90 | - | $st = $this->con->prepare($query); |
90 | + | $st = $this |
91 | - | $st->execute($data); |
91 | + | ->con |
92 | - | $this->success = true; |
92 | + | ->prepare($query); |
93 | - | } |
93 | + | $st->execute($data); |
94 | - | catch (PDOException $e) { |
94 | + | $this->success = true; |
95 | - | $this->success = false; |
95 | + | } |
96 | - | $this->message = $e->getMessage(); |
96 | + | catch(PDOException $e) |
97 | - | } |
97 | + | { |
98 | - | } |
98 | + | $this->success = false; |
99 | $this->message = "error:".$e->getMessage()."\nSql:".$query; | |
100 | - | public function update($table, $data, $where, $condition='AND') { |
100 | + | } |
101 | - | try { |
101 | + | } |
102 | - | $field = $value = ''; |
102 | + | public function insert($table, $data) |
103 | - | foreach($data as $f=>$v) { |
103 | + | { |
104 | - | $field .= ','.str_replace(':','',$f).'='.$f; |
104 | + | try |
105 | - | } |
105 | + | { |
106 | - | foreach($where as $f=>$v) { |
106 | + | $field = $value = ''; |
107 | - | $value .= ' && '.str_replace(':','',$f).'='.$f; |
107 | + | foreach ($data as $f => $v) |
108 | - | } |
108 | + | { |
109 | - | $field = substr($field,1); |
109 | + | $field .= ',' . str_replace(':', '', $v); |
110 | - | $value = substr($value,4); |
110 | + | $value .= ',' . $v; |
111 | - | $value = str_replace('&&',$condition,$value); |
111 | + | } |
112 | - | $query = 'UPDATE '.$table.' SET '.$field.' WHERE '.$value; |
112 | + | $field = substr($field, 1); |
113 | - | $st = $this->con->prepare($query); |
113 | + | $value = substr($value, 1); |
114 | - | $st->execute(array_merge($data,$where)); |
114 | + | $query = 'INSERT INTO ' . $table . ' (' . $field . ') VALUES (' . $value . ')'; |
115 | - | $this->success = true; |
115 | + | $st = $this |
116 | - | } |
116 | + | ->con |
117 | - | catch (PDOException $e) { |
117 | + | ->prepare($query); |
118 | - | $this->success = false; |
118 | + | $st->execute($data); |
119 | - | $this->message = $e->getMessage(); |
119 | + | $this->success = true; |
120 | - | } |
120 | + | } |
121 | - | } |
121 | + | catch(PDOException $e) |
122 | { | |
123 | - | public function delete($table, $where, $condition='AND') { |
123 | + | $this->success = false; |
124 | - | try { |
124 | + | $this->message = $e->getMessage(); |
125 | - | $value = ''; |
125 | + | } |
126 | - | foreach($where as $f=>$v) { |
126 | + | } |
127 | - | $value .= ' && '.str_replace(':','',$f).'='.$f; |
127 | + | |
128 | - | } |
128 | + | public function update($table, $data, $where, $condition = 'AND') |
129 | - | $value = substr($value,4); |
129 | + | { |
130 | - | $value = str_replace('&&',$condition,$value); |
130 | + | try |
131 | - | $query = 'DELETE FROM '.$table.' WHERE '.$value; |
131 | + | { |
132 | - | $st = $this->con->prepare($query); |
132 | + | $field = $value = ''; |
133 | - | $st->execute($where); |
133 | + | foreach ($data as $f => $v) |
134 | - | $this->success = true; |
134 | + | { |
135 | - | } |
135 | + | $field .= ',' . str_replace(':', '', $f) . '=' . $f; |
136 | - | catch (PDOException $e) { |
136 | + | } |
137 | - | $this->success = false; |
137 | + | foreach ($where as $f => $v) |
138 | - | $this->message = $e->getMessage(); |
138 | + | { |
139 | - | } |
139 | + | $value .= ' && ' . str_replace(':', '', $f) . '=' . $f; |
140 | - | } |
140 | + | } |
141 | $field = substr($field, 1); | |
142 | - | public function select($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') { |
142 | + | $value = substr($value, 4); |
143 | - | try { |
143 | + | $value = str_replace('&&', $condition, $value); |
144 | - | $output = array(); |
144 | + | $query = 'UPDATE ' . $table . ' SET ' . $field . ' WHERE ' . $value; |
145 | - | $value = ''; |
145 | + | $st = $this |
146 | - | $query = 'SELECT '.$data.' FROM '.$table; |
146 | + | ->con |
147 | - | if($where!=null) { |
147 | + | ->prepare($query); |
148 | - | foreach($where as $f=>$v) { |
148 | + | $st->execute(array_merge($data, $where)); |
149 | - | $value .= ' && '.str_replace(':','',$f).'='.$f; |
149 | + | $this->success = true; |
150 | - | } |
150 | + | } |
151 | - | $value = ' WHERE '.substr($value,4); |
151 | + | catch(PDOException $e) |
152 | - | $value = str_replace('&&',$condition,$value); |
152 | + | { |
153 | - | $query .= $value; |
153 | + | $this->success = false; |
154 | - | } |
154 | + | $this->message = $e->getMessage(); |
155 | - | ($group!=null) ? $query .= ' GROUP BY '.$group :null; |
155 | + | } |
156 | - | ($order!=null) ? $query .= ' ORDER BY '.$order :null; |
156 | + | } |
157 | - | ($limit!=null) ? $query .= ' LIMIT '.$limit :null; |
157 | + | |
158 | - | $st = $this->con->prepare($query); |
158 | + | public function delete($table, $where, $condition = 'AND') |
159 | - | $st->execute($where); |
159 | + | { |
160 | - | $this->success = true; |
160 | + | try |
161 | - | while($result = $st->fetch()) { |
161 | + | { |
162 | - | $output[] = $result; |
162 | + | $value = ''; |
163 | - | } |
163 | + | foreach ($where as $f => $v) |
164 | - | $output = str_replace("'","'",$output); |
164 | + | { |
165 | - | return $output; |
165 | + | $value .= ' && ' . str_replace(':', '', $f) . '=' . $f; |
166 | - | } |
166 | + | } |
167 | - | catch (PDOException $e) { |
167 | + | $value = substr($value, 4); |
168 | - | $this->success = false; |
168 | + | $value = str_replace('&&', $condition, $value); |
169 | - | $this->message = $e->getMessage(); |
169 | + | $query = 'DELETE FROM ' . $table . ' WHERE ' . $value; |
170 | - | } |
170 | + | $st = $this |
171 | - | } |
171 | + | ->con |
172 | ->prepare($query); | |
173 | - | public function selectAssoc($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') { |
173 | + | $st->execute($where); |
174 | - | try { |
174 | + | $this->success = true; |
175 | - | $output = array(); |
175 | + | } |
176 | - | $value = ''; |
176 | + | catch(PDOException $e) |
177 | - | $query = 'SELECT '.$data.' FROM '.$table; |
177 | + | { |
178 | - | if($where!=null) { |
178 | + | $this->success = false; |
179 | - | foreach($where as $f=>$v) { |
179 | + | $this->message = $e->getMessage(); |
180 | - | $value .= ' && '.str_replace(':','',$f).'='.$f; |
180 | + | } |
181 | - | } |
181 | + | } |
182 | - | $value = ' WHERE '.substr($value,4); |
182 | + | |
183 | - | $value = str_replace('&&',$condition,$value); |
183 | + | public function select($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND') |
184 | - | $query .= $value; |
184 | + | { |
185 | - | } |
185 | + | try |
186 | - | ($group!=null) ? $query .= ' GROUP BY '.$group :null; |
186 | + | { |
187 | - | ($order!=null) ? $query .= ' ORDER BY '.$order :null; |
187 | + | $output = array(); |
188 | - | ($limit!=null) ? $query .= ' LIMIT '.$limit :null; |
188 | + | $value = ''; |
189 | - | $st = $this->con->prepare($query); |
189 | + | $query = 'SELECT ' . $data . ' FROM ' . $table; |
190 | - | $st->execute($where); |
190 | + | if ($where != null) |
191 | - | $this->success = true; |
191 | + | { |
192 | - | while($result = $st->fetch(PDO::FETCH_ASSOC)) { |
192 | + | foreach ($where as $f => $v) |
193 | - | $output[] = $result; |
193 | + | { |
194 | - | } |
194 | + | $value .= ' && ' . str_replace(':', '', $f) . '=' . $f; |
195 | - | $output = str_replace("'","'",$output); |
195 | + | } |
196 | - | return $output; |
196 | + | $value = ' WHERE ' . substr($value, 4); |
197 | - | } |
197 | + | $value = str_replace('&&', $condition, $value); |
198 | - | catch (PDOException $e) { |
198 | + | $query .= $value; |
199 | - | $this->success = false; |
199 | + | } |
200 | - | $this->message = $e->getMessage(); |
200 | + | ($group != null) ? $query .= ' GROUP BY ' . $group : null; |
201 | - | } |
201 | + | ($order != null) ? $query .= ' ORDER BY ' . $order : null; |
202 | - | } |
202 | + | ($limit != null) ? $query .= ' LIMIT ' . $limit : null; |
203 | $st = $this | |
204 | - | public function fetch($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') { |
204 | + | ->con |
205 | - | try { |
205 | + | ->prepare($query); |
206 | - | $value = ''; |
206 | + | $st->execute($where); |
207 | - | $query = 'SELECT '.$data.' FROM '.$table; |
207 | + | $this->success = true; |
208 | - | if($where!=null) { |
208 | + | while ($result = $st->fetch()) |
209 | - | foreach($where as $f=>$v) { |
209 | + | { |
210 | - | $value .= ' && '.str_replace(':','',$f).'='.$f; |
210 | + | $output[] = $result; |
211 | - | } |
211 | + | } |
212 | - | $value = ' WHERE '.substr($value,4); |
212 | + | $output = str_replace("'", "'", $output); |
213 | - | $value = str_replace('&&',$condition,$value); |
213 | + | return $output; |
214 | - | $query .= $value; |
214 | + | } |
215 | - | } |
215 | + | catch(PDOException $e) |
216 | - | ($group!=null) ? $query .= ' GROUP BY '.$group :null; |
216 | + | { |
217 | - | ($order!=null) ? $query .= ' ORDER BY '.$order :null; |
217 | + | $this->success = false; |
218 | - | ($limit!=null) ? $query .= ' LIMIT '.$limit :null; |
218 | + | $this->message = $e->getMessage(); |
219 | - | $st = $this->con->prepare($query); |
219 | + | } |
220 | - | $st->execute($where); |
220 | + | } |
221 | - | $output = $st->fetch(); |
221 | + | |
222 | - | $this->success = true; |
222 | + | public function selectAssoc($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND') |
223 | - | if($output=='') { |
223 | + | { |
224 | - | $output = array(); |
224 | + | try |
225 | - | $field = explode(',',$data); |
225 | + | { |
226 | - | $i = 0; |
226 | + | $output = array(); |
227 | - | foreach($field as $fil) { |
227 | + | $value = ''; |
228 | - | $output = array_merge($output,array($i=>'')); |
228 | + | $query = 'SELECT ' . $data . ' FROM ' . $table; |
229 | - | $output = array_merge($output,array($fil=>'')); |
229 | + | if ($where != null) |
230 | - | $i++; |
230 | + | { |
231 | - | } |
231 | + | foreach ($where as $f => $v) |
232 | - | } else { |
232 | + | { |
233 | - | $output = str_replace("'","'",$output); |
233 | + | $value .= ' && ' . str_replace(':', '', $f) . '=' . $f; |
234 | - | } |
234 | + | } |
235 | - | return $output; |
235 | + | $value = ' WHERE ' . substr($value, 4); |
236 | - | } |
236 | + | $value = str_replace('&&', $condition, $value); |
237 | - | catch (PDOException $e) { |
237 | + | $query .= $value; |
238 | - | $this->success = false; |
238 | + | } |
239 | - | $this->message = $e->getMessage(); |
239 | + | ($group != null) ? $query .= ' GROUP BY ' . $group : null; |
240 | - | } |
240 | + | ($order != null) ? $query .= ' ORDER BY ' . $order : null; |
241 | - | } |
241 | + | ($limit != null) ? $query .= ' LIMIT ' . $limit : null; |
242 | $st = $this | |
243 | - | public function fetchAssoc($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') { |
243 | + | ->con |
244 | - | try { |
244 | + | ->prepare($query); |
245 | - | $value = ''; |
245 | + | $st->execute($where); |
246 | - | $query = 'SELECT '.$data.' FROM '.$table; |
246 | + | $this->success = true; |
247 | - | if($where!=null) { |
247 | + | while ($result = $st->fetch(PDO::FETCH_ASSOC)) |
248 | - | foreach($where as $f=>$v) { |
248 | + | { |
249 | - | $value .= ' && '.str_replace(':','',$f).'='.$f; |
249 | + | $output[] = $result; |
250 | - | } |
250 | + | } |
251 | - | $value = ' WHERE '.substr($value,4); |
251 | + | $output = str_replace("'", "'", $output); |
252 | - | $value = str_replace('&&',$condition,$value); |
252 | + | return $output; |
253 | - | $query .= $value; |
253 | + | } |
254 | - | } |
254 | + | catch(PDOException $e) |
255 | - | ($group!=null) ? $query .= ' GROUP BY '.$group :null; |
255 | + | { |
256 | - | ($order!=null) ? $query .= ' ORDER BY '.$order :null; |
256 | + | $this->success = false; |
257 | - | ($limit!=null) ? $query .= ' LIMIT '.$limit :null; |
257 | + | $this->message = $e->getMessage(); |
258 | - | $st = $this->con->prepare($query); |
258 | + | } |
259 | - | $st->execute($where); |
259 | + | } |
260 | - | $output = $st->fetch(PDO::FETCH_ASSOC); |
260 | + | |
261 | - | $this->success = true; |
261 | + | public function fetch($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND') |
262 | - | if($output=='') { |
262 | + | { |
263 | - | $output = array(); |
263 | + | try |
264 | - | $field = explode(',',$data); |
264 | + | { |
265 | - | $i = 0; |
265 | + | $value = ''; |
266 | - | foreach($field as $fil) { |
266 | + | $query = 'SELECT ' . $data . ' FROM ' . $table; |
267 | - | $output = array_merge($output,array($i=>'')); |
267 | + | if ($where != null) |
268 | - | $output = array_merge($output,array($fil=>'')); |
268 | + | { |
269 | - | $i++; |
269 | + | foreach ($where as $f => $v) |
270 | - | } |
270 | + | { |
271 | - | } else { |
271 | + | $value .= ' && ' . str_replace(':', '', $f) . '=' . $f; |
272 | - | $output = str_replace("'","'",$output); |
272 | + | } |
273 | - | } |
273 | + | $value = ' WHERE ' . substr($value, 4); |
274 | - | return $output; |
274 | + | $value = str_replace('&&', $condition, $value); |
275 | - | } |
275 | + | $query .= $value; |
276 | - | catch (PDOException $e) { |
276 | + | } |
277 | - | $this->success = false; |
277 | + | ($group != null) ? $query .= ' GROUP BY ' . $group : null; |
278 | - | $this->message = $e->getMessage(); |
278 | + | ($order != null) ? $query .= ' ORDER BY ' . $order : null; |
279 | - | } |
279 | + | ($limit != null) ? $query .= ' LIMIT ' . $limit : null; |
280 | - | } |
280 | + | $st = $this |
281 | ->con | |
282 | - | public function fieldcount($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') { |
282 | + | ->prepare($query); |
283 | - | try { |
283 | + | $st->execute($where); |
284 | - | $value = ''; |
284 | + | $output = $st->fetch(); |
285 | - | $query = 'SELECT COUNT('.$data.') FROM '.$table; |
285 | + | $this->success = true; |
286 | - | if($where!=null) { |
286 | + | if ($output == '') |
287 | - | foreach($where as $f=>$v) { |
287 | + | { |
288 | - | $value .= ' && '.str_replace(':','',$f).'='.$f; |
288 | + | $output = array(); |
289 | - | } |
289 | + | $field = explode(',', $data); |
290 | - | $value = ' WHERE '.substr($value,4); |
290 | + | $i = 0; |
291 | - | $value = str_replace('&&',$condition,$value); |
291 | + | foreach ($field as $fil) |
292 | - | $query .= $value; |
292 | + | { |
293 | - | } |
293 | + | $output = array_merge($output, array( |
294 | - | ($group!=null) ? $query .= ' GROUP BY '.$group :null; |
294 | + | $i => '' |
295 | - | ($order!=null) ? $query .= ' ORDER BY '.$order :null; |
295 | + | )); |
296 | - | ($limit!=null) ? $query .= ' LIMIT '.$limit :null; |
296 | + | $output = array_merge($output, array( |
297 | - | $st = $this->con->prepare($query); |
297 | + | $fil => '' |
298 | - | $st->execute($where); |
298 | + | )); |
299 | - | $this->success = true; |
299 | + | $i++; |
300 | - | $output = $st->columnCount(); |
300 | + | } |
301 | - | return $output; |
301 | + | } |
302 | - | } |
302 | + | else |
303 | - | catch (PDOException $e) { |
303 | + | { |
304 | - | $this->success = false; |
304 | + | $output = str_replace("'", "'", $output); |
305 | - | $this->message = $e->getMessage(); |
305 | + | } |
306 | - | } |
306 | + | return $output; |
307 | - | } |
307 | + | } |
308 | catch(PDOException $e) | |
309 | - | public function rowcount($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') { |
309 | + | { |
310 | - | try { |
310 | + | $this->success = false; |
311 | - | $value = ''; |
311 | + | $this->message = $e->getMessage(); |
312 | - | $query = 'SELECT COUNT('.$data.') FROM '.$table; |
312 | + | } |
313 | - | if($where!=null) { |
313 | + | } |
314 | - | foreach($where as $f=>$v) { |
314 | + | |
315 | - | $value .= ' && '.str_replace(':','',$f).'='.$f; |
315 | + | public function fetchAssoc($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND') |
316 | - | } |
316 | + | { |
317 | - | $value = ' WHERE '.substr($value,4); |
317 | + | try |
318 | - | $value = str_replace('&&',$condition,$value); |
318 | + | { |
319 | - | $query .= $value; |
319 | + | $value = ''; |
320 | - | } |
320 | + | $query = 'SELECT ' . $data . ' FROM ' . $table; |
321 | - | ($group!=null) ? $query .= ' GROUP BY '.$group :null; |
321 | + | if ($where != null) |
322 | - | ($order!=null) ? $query .= ' ORDER BY '.$order :null; |
322 | + | { |
323 | - | ($limit!=null) ? $query .= ' LIMIT '.$limit :null; |
323 | + | foreach ($where as $f => $v) |
324 | - | $st = $this->con->prepare($query); |
324 | + | { |
325 | - | $st->execute($where); |
325 | + | $value .= ' && ' . str_replace(':', '', $f) . '=' . $f; |
326 | - | $this->success = true; |
326 | + | } |
327 | - | $output = $st->fetchColumn(0); |
327 | + | $value = ' WHERE ' . substr($value, 4); |
328 | - | return $output; |
328 | + | $value = str_replace('&&', $condition, $value); |
329 | - | } |
329 | + | $query .= $value; |
330 | - | catch (PDOException $e) { |
330 | + | } |
331 | - | $this->success = false; |
331 | + | ($group != null) ? $query .= ' GROUP BY ' . $group : null; |
332 | - | $this->message = $e->getMessage(); |
332 | + | ($order != null) ? $query .= ' ORDER BY ' . $order : null; |
333 | - | } |
333 | + | ($limit != null) ? $query .= ' LIMIT ' . $limit : null; |
334 | - | } |
334 | + | $st = $this |
335 | ->con | |
336 | - | public function rowsum($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') { |
336 | + | ->prepare($query); |
337 | - | try { |
337 | + | $st->execute($where); |
338 | - | $value = ''; |
338 | + | $output = $st->fetch(PDO::FETCH_ASSOC); |
339 | - | $query = 'SELECT SUM('.$data.') FROM '.$table; |
339 | + | $this->success = true; |
340 | - | if($where!=null) { |
340 | + | if ($output == '') |
341 | - | foreach($where as $f=>$v) { |
341 | + | { |
342 | - | $value .= ' && '.str_replace(':','',$f).'='.$f; |
342 | + | $output = array(); |
343 | - | } |
343 | + | $field = explode(',', $data); |
344 | - | $value = ' WHERE '.substr($value,4); |
344 | + | $i = 0; |
345 | - | $value = str_replace('&&',$condition,$value); |
345 | + | foreach ($field as $fil) |
346 | - | $value = str_replace("!=","<>",$value); |
346 | + | { |
347 | - | $query .= $value; |
347 | + | $output = array_merge($output, array( |
348 | - | } |
348 | + | $i => '' |
349 | - | ($group!=null) ? $query .= ' GROUP BY '.$group :null; |
349 | + | )); |
350 | - | ($order!=null) ? $query .= ' ORDER BY '.$order :null; |
350 | + | $output = array_merge($output, array( |
351 | - | ($limit!=null) ? $query .= ' LIMIT '.$limit :null; |
351 | + | $fil => '' |
352 | - | $st = $this->con->prepare($query); |
352 | + | )); |
353 | - | $st->execute($where); |
353 | + | $i++; |
354 | - | $this->success = true; |
354 | + | } |
355 | - | $output = $st->fetchColumn(0); |
355 | + | } |
356 | - | return $output; |
356 | + | else |
357 | - | } |
357 | + | { |
358 | - | catch (PDOException $e) { |
358 | + | $output = str_replace("'", "'", $output); |
359 | - | $this->success = false; |
359 | + | } |
360 | - | $this->message = $e->getMessage(); |
360 | + | return $output; |
361 | - | } |
361 | + | } |
362 | - | } |
362 | + | catch(PDOException $e) |
363 | { | |
364 | - | public function truncate($table) { |
364 | + | $this->success = false; |
365 | - | try { |
365 | + | $this->message = $e->getMessage(); |
366 | - | $query = 'TRUNCATE '.$table; |
366 | + | } |
367 | - | $st = $this->con->prepare($query); |
367 | + | } |
368 | - | $st->execute(); |
368 | + | |
369 | - | $this->success = true; |
369 | + | public function fieldcount($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND') |
370 | - | } |
370 | + | { |
371 | - | catch (PDOException $e) { |
371 | + | try |
372 | - | $this->success = false; |
372 | + | { |
373 | - | $this->message = $e->getMessage(); |
373 | + | $value = ''; |
374 | - | } |
374 | + | $query = 'SELECT COUNT(' . $data . ') FROM ' . $table; |
375 | - | } |
375 | + | if ($where != null) |
376 | { | |
377 | - | public function query($query) { |
377 | + | foreach ($where as $f => $v) |
378 | - | try { |
378 | + | { |
379 | - | $st = $this->con->prepare($query); |
379 | + | $value .= ' && ' . str_replace(':', '', $f) . '=' . $f; |
380 | - | $st->execute(); |
380 | + | } |
381 | - | return $st->fetchAll(); |
381 | + | $value = ' WHERE ' . substr($value, 4); |
382 | - | $this->success = true; |
382 | + | $value = str_replace('&&', $condition, $value); |
383 | - | } |
383 | + | $query .= $value; |
384 | - | catch (PDOException $e) { |
384 | + | } |
385 | - | $this->success = false; |
385 | + | ($group != null) ? $query .= ' GROUP BY ' . $group : null; |
386 | - | $this->message = $e->getMessage(); |
386 | + | ($order != null) ? $query .= ' ORDER BY ' . $order : null; |
387 | - | } |
387 | + | ($limit != null) ? $query .= ' LIMIT ' . $limit : null; |
388 | - | } |
388 | + | $st = $this |
389 | ->con | |
390 | - | public function dbsize() { |
390 | + | ->prepare($query); |
391 | - | $output = $this->query("SELECT sum( data_length + index_length ) AS dbsize FROM information_schema.TABLES WHERE table_schema='".$this->debe."'"); |
391 | + | $st->execute($where); |
392 | - | return $output[0][0]; |
392 | + | $this->success = true; |
393 | - | } |
393 | + | $output = $st->columnCount(); |
394 | - | } |
394 | + | return $output; |
395 | } | |
396 | catch(PDOException $e) | |
397 | { | |
398 | $this->success = false; | |
399 | $this->message = $e->getMessage(); | |
400 | } | |
401 | } | |
402 | ||
403 | public function rowcount($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND') | |
404 | { | |
405 | try | |
406 | { | |
407 | $value = ''; | |
408 | $query = 'SELECT COUNT(' . $data . ') FROM ' . $table; | |
409 | if ($where != null) | |
410 | { | |
411 | foreach ($where as $f => $v) | |
412 | { | |
413 | $value .= ' && ' . str_replace(':', '', $f) . '=' . $f; | |
414 | } | |
415 | $value = ' WHERE ' . substr($value, 4); | |
416 | $value = str_replace('&&', $condition, $value); | |
417 | $query .= $value; | |
418 | } | |
419 | ($group != null) ? $query .= ' GROUP BY ' . $group : null; | |
420 | ($order != null) ? $query .= ' ORDER BY ' . $order : null; | |
421 | ($limit != null) ? $query .= ' LIMIT ' . $limit : null; | |
422 | $st = $this | |
423 | ->con | |
424 | ->prepare($query); | |
425 | $st->execute($where); | |
426 | $this->success = true; | |
427 | $output = $st->fetchColumn(0); | |
428 | return $output; | |
429 | } | |
430 | catch(PDOException $e) | |
431 | { | |
432 | $this->success = false; | |
433 | $this->message = $e->getMessage(); | |
434 | } | |
435 | } | |
436 | ||
437 | public function rowsum($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND') | |
438 | { | |
439 | try | |
440 | { | |
441 | $value = ''; | |
442 | $query = 'SELECT SUM(' . $data . ') FROM ' . $table; | |
443 | if ($where != null) | |
444 | { | |
445 | foreach ($where as $f => $v) | |
446 | { | |
447 | $value .= ' && ' . str_replace(':', '', $f) . '=' . $f; | |
448 | } | |
449 | $value = ' WHERE ' . substr($value, 4); | |
450 | $value = str_replace('&&', $condition, $value); | |
451 | $value = str_replace("!=", "<>", $value); | |
452 | $query .= $value; | |
453 | } | |
454 | ($group != null) ? $query .= ' GROUP BY ' . $group : null; | |
455 | ($order != null) ? $query .= ' ORDER BY ' . $order : null; | |
456 | ($limit != null) ? $query .= ' LIMIT ' . $limit : null; | |
457 | $st = $this | |
458 | ->con | |
459 | ->prepare($query); | |
460 | $st->execute($where); | |
461 | $this->success = true; | |
462 | $output = $st->fetchColumn(0); | |
463 | return $output; | |
464 | } | |
465 | catch(PDOException $e) | |
466 | { | |
467 | $this->success = false; | |
468 | $this->message = $e->getMessage(); | |
469 | } | |
470 | } | |
471 | ||
472 | public function truncate($table) | |
473 | { | |
474 | try | |
475 | { | |
476 | $query = 'TRUNCATE ' . $table; | |
477 | $st = $this | |
478 | ->con | |
479 | ->prepare($query); | |
480 | $st->execute(); | |
481 | $this->success = true; | |
482 | } | |
483 | catch(PDOException $e) | |
484 | { | |
485 | $this->success = false; | |
486 | $this->message = $e->getMessage(); | |
487 | } | |
488 | } | |
489 | ||
490 | public function query($query) | |
491 | { | |
492 | try | |
493 | { | |
494 | $st = $this | |
495 | ->con | |
496 | ->prepare($query); | |
497 | $st->execute(); | |
498 | return $st->fetchAll(); | |
499 | $this->success = true; | |
500 | } | |
501 | catch(PDOException $e) | |
502 | { | |
503 | $this->success = false; | |
504 | $this->message = $e->getMessage(); | |
505 | } | |
506 | } | |
507 | ||
508 | public function dbsize() | |
509 | { | |
510 | $output = $this->query("SELECT sum( data_length + index_length ) AS dbsize FROM information_schema.TABLES WHERE table_schema='" . $this->debe . "'"); | |
511 | return $output[0][0]; | |
512 | } | |
513 | } | |
514 | ?> | |
515 |