Yet another SQL Class

PHP  (06.05.2010 18:58) Die 1.000.000ste SQL PHP Klasse.
Befindet sich noch in der Entwicklung. Nutzung auf eigene Gefahr.

Quellcode (ausblenden | aufklappen)

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
 
if(version_compare(PHP_VERSION, '5', '<')) {
   trigger_error("The Class dbconnect requires PHP 5.0 or higher!", E_USER_ERROR);
}
 
class dbconnect {
 
   private $connection;
   private $debug;
   private $logging;
 
   private $last_error;
   private $last_error_number;
   private $command_count;
 
   function __construct() {
      $this->connection=false;
      $this->debug=false;
      $this->logging=array();
      $this->last_error = false;
      $this->last_error_number = false;
      $this->command_count = array();
 
      if(!(function_exists("mysql_connect") && function_exists("mysql_query"))) {
         trigger_error("MySQL not found! Please check your PHP Installation!", E_USER_ERROR);
      }
   }
 
   function  __destruct() {
      $this->disconnect();
   }
   
   function debug($b) {
      $this->debug = ($b===true ? true : false);
   }
 
 
   public function get_log($fancy=false) {
      $style_cell="border:1px solid black; font-size:12px;";
 
      if($this->debug===false) {
         trigger_error("Debug is not active!", E_USER_NOTICE);
         return false;
      } elseif($fancy===false) {
         return $this->logging;
      } else {
         $r = "<h1 style='margin:0px;'>dbconnect log</h1>";
         foreach($this->command_count as $name => $count) {
            $r .= "<b>".$name.":</b> ".$count."; ";
         }
         $r .=  "<table style='".$style_cell." border-collapse:collapse; width:100%;'>";
            $r .= "<tr>";
               $r .= "<th style='".$style_cell." width:100px;'>Typ</th>";
               $r .= "<th style='".$style_cell." width:300px;'>File</th>";
               $r .= "<th style='".$style_cell."'>Message/Query</th>";
               $r .= "<th style='".$style_cell." width:100px;'>Rows</th>";
               $r .= "<th style='".$style_cell." width:100px;''>Fields</th>";
               $r .= "<th style='".$style_cell." width:100px;''>Aff. Rows</th>";
            $r .= "</tr>";
            foreach($this->logging as $l) {
               $r .= "<tr>";
                  $r .= "<td style='".$style_cell."'>".$l['type']."</td>";
                  $r .= "<td style='".$style_cell."'>".$l['file'].":".$l['file_line']."</td>";
                  $r .= "<td style='".$style_cell."'".($l['type']!="Query" && $l['type']!="Command" ? " colspan='4'" : "").">";
                     $r .= ($l['msg']===false ? $l['command'] : $l['msg']);
                  $r .= "</td>";
                  if($l['type']=="Query" || $l['type']=="Command") {
                     $r .= "<td style='".$style_cell."'>".($l['num_rows']===false ? "<em>false</em>" : $l['num_rows'])."</td>";
                     $r .= "<td style='".$style_cell."'>".($l['num_fields']===false ? "<em>false</em>" : $l['num_fields'])."</td>";
                     $r .= "<td style='".$style_cell."'>".($l['num_affected']===false ? "<em>false</em>" : $l['num_affected'])."</td>";
                  }
               $r .= "</tr>";
               if($l['errno']!=0) {
                  $r .= "<tr>";
                     $r .= "<td style='".$style_cell." color:red;'>Error</td>";
                     $r .= "<td style='".$style_cell." color:red;' colspan='4'>".$l['errno'].": ".$l['error']."</td>";
                  $r .= "</tr>";
               }
            }
         $r .= "</table>";
 
         return $r;
      }
   }
 
 
 
   private function &log_newentry($command=false) {
      if($command!==false) {
         $keyword = strtolower(reset(explode(" ", $command)));
         if(!isset($this->command_count['all'])) $this->command_count['all']=0;
         if(!isset($this->command_count[$keyword])) $this->command_count[$keyword]=0;
         $this->command_count['all']++;
         $this->command_count[$keyword]++;
      }
 
      $backtrace = debug_backtrace();
      $temp = array(
         "type" => false,
         "msg" => false,
         "command" => $command,
         "num_rows" => false,
         "num_fields" => false,
         "num_affected" => false,
         "error" => ($this->connection===false || mysql_error($this->connection)=="" ? false : mysql_error($this->connection)),
         "errno" => ($this->connection===false || mysql_errno($this->connection)==0 ? false : mysql_errno($this->connection)),
         "file" => $backtrace[2]['file'],
         "file_line" => $backtrace[2]['line'],
         "time" => microtime()
      );
      $this->logging[] = $temp;
      return $this->logging[(count($this->logging)-1)];
   }
 
 
   /**
    * Loggt ein Ereignis (zB fehlgeschlagener Connect)
    * @param String $type
    * @param String $msg
    * @return void
    */
   private function log($type, $msg) {
      if($this->debug===true) {
         $log =& $this->log_newentry();
         $log['type'] = $type;
         $log['msg'] = $msg;
      }
   }
 
 
   /**
    * Loggt ein SQL Query
    * @param String $query
    * @param Object $result
    */
   private function log_query($query, $result=false) {
      if($this->debug===true) {
         $log =& $this->log_newentry($query);
         $log['type'] = "Query";
         if($result instanceof dbconnect_result) {
            $log['num_rows'] = $result->get_num_rows();
            $log['num_fields'] = $result->get_num_fields();
         }
      }
   }
 
 
   private function log_command($command) {
      if($this->debug===true) {
         $log =& $this->log_newentry($command);
         $log['type'] = "Command";
         $log['num_affected'] = $this->get_affected_rows();
      }
   }
 
 
   /**
    * Zu einem MySQL Server verbinden
    * @param String $username
    * @param String $password
    * @param String $hostname
    * @param int $port
    * @return false
    */
   public function connect($username, $password, $hostname="localhost", $port=3306) {
      if($this->connection===false) {
         $this->log("Connection", "Connecting to ".$hostname.":".((int)$port));
         $this->connection = @mysql_connect($hostname.":".((int)$port), $username, $password);
         if($this->connection===false) {
            $this->log("Connection", "Connection to ".$hostname.":".((int)$port)." failed");
            return false;
         } else {
            $this->log("Connection", "Connection to ".$hostname.":".((int)$port)." successfully");
            return true;
         }
      } else {
         return false;
      }
   }
 
 
   /**
    * MySQL Verbindung trennen
    * @return bool
    */
   public function disconnect() {
      if($this->connection!==false) {
         $this->log("Connection", "Connection closed");
         @mysql_close($this->connection);
         return true;
      } else {
         return false;
      }
   }
 
 
   /**
    * Eine Datenbank auswaehlen
    * @param String $db
    * @return bool
    */
   public function set_db($db) {
      if(@mysql_select_db($db, $this->connection)) {
         $this->log("Connection", "Setting Database ".$db);
         return true;
      } else {
         $this->log("Connection", "Database ".$db." not found");
         return false;
      }
   }
 
 
   /**
    * Fuehrt ein Query aus
    * Beispiel: $db->query("select * from table where field='%s'", "name");
    * @param String $query
    * @return ressource,bool
    */
   public function query($query) {
      $args = func_get_args();
      unset($args[0]);
      if(count($args)>0) {
         $args = array_map("mysql_real_escape_string", $args);
         $query = vsprintf($query, $args);
      }
      if($query!==false) {
         $query = trim($query);
         $result = @mysql_query($query, $this->connection);
         if(is_resource($result)) {
            $dbres = new dbconnect_result($result);
            $this->log_query($query, $dbres);
            return $dbres;
         } else {
            $this->log_command($query);
            return $result;
         }
      } else {
         return false;
      }
   }
 
 
   public function get_affected_rows() {
      return @mysql_affected_rows($this->connection);
   }
 
}
 
 
class dbconnect_result {
 
   private $result;
   private $num_rows;
   private $num_fields;
 
   function  __construct($res) {
      if(is_resource($res)) {
         $this->result = $res;
         $this->num_rows = @mysql_num_rows($this->result);
         $this->num_fields = @mysql_num_fields($this->result);
      } else {
         trigger_error("\$res is not a valid MySQL link ressource!", E_USER_ERROR);
      }
   }
 
   function  __destruct() {
      @mysql_free_result($this->result);
   }
 
 
   public function get_num_rows() {
      return $this->num_rows;
   }
 
 
   public function get_num_fields() {
      return $this->num_fields;
   }
 
 
   /**
    * Setzt den Row-Pointer im MySQL Result
    * @param int $i Position
    * @return bool
    */
   public function set_result_pointer($i=0) {
      if(@mysql_data_seek($this->result, $i)===true) {
         return true;
      } else {
         return false;
      }
   }
 
 
   /**
    * Liefert einen Datensatz als Array
    * @param int $type MYSQL_NUM=>numerisches Array, MYSQL_ASSOC=>assoziatives Array, MYSQL_BOTH=>beides
    * @return mixed array, false
    */
   public function fetch_array($type=MYSQL_BOTH) {
      if(!($type==MYSQL_BOTH || $type==MYSQL_ASSOC || $type==MYSQL_NUM)) {
         trigger_error("No valid type given!", E_USER_ERROR);
      }
      switch($type) {
         case MYSQL_NUM: return @mysql_fetch_array($this->result, MYSQL_NUM); break;
         case MYSQL_ASSOC: return @mysql_fetch_array($this->result, MYSQL_ASSOC); break;
         case MYSQL_BOTH: default: return @mysql_fetch_array($this->result, MYSQL_NUM); break;
      }
   }
 
 
   /**
    * Liefert einen Datensatz als numerisches Array
    * @return mixed array, false
    */
   public function fetch_row() {
      return $this->fetch_array(MYSQL_NUM);
   }
 
 
   /**
    * Liefert einen Datensatz als assoziatives Array
    * @return mixed array, false
    */
   public function fetch_assoc() {
      return $this->fetch_array(MYSQL_ASSOC);
   }
 
 
   /**
    * Liefert ein zweidimensionales Array aller Datensaetze
    * @param int $type MYSQL_NUM=>numerisches Array, MYSQL_ASSOC=>assoziatives Array, MYSQL_BOTH=>beides
    * @return mixed Array, false
    */
   public function fetch_all($type=MYSQL_BOTH) {
      if(!($type==MYSQL_BOTH || $type==MYSQL_ASSOC || $type==MYSQL_NUM)) {
         trigger_error("No valid type given!", E_USER_ERROR);
      }
      $this->set_result_pointer();
      $return = array();
 
      while($row = $this->fetch_array($type)) {
         $return[] = $row;
      }
      return $return;
   }
 
 
   /**
    * Liefert ein zweidimensionales numerisches Array aller Datensaetze
    * @return mixed Array, false
    */
   public function fetch_all_row() {
      return $this->fetch_all(MYSQL_NUM);
   }
 
 
   /**
    * Liefert ein zweidimensionales assoziatives Array aller Datensaetze
    * @return mixed Array, false
    */
   public function fetch_all_assoc() {
      return $this->fetch_all(MYSQL_ASSOC);
   }
 
 
}
 
 
?>