source: Base/Database.php

Last change on this file was 1, checked in by george, 14 years ago
  • Přidáno: Výchozí verze systému.
File size: 3.2 KB
Line 
1<?php
2
3// Extended database class
4// Date: 2009-02-16
5
6class Database extends mysqli
7{
8 var $Prefix = '';
9 var $Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
10 var $ShowSQLQuery = false;
11 var $ShowSQLError = false;
12
13 function query($Query)
14 {
15
16 if($this->ShowSQLQuery)
17 {
18 if(isset($_SERVER['REMOTE_ADDR'])) echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.'</div>');
19 else echo($Query."\n");
20 }
21 $Result = parent::query($Query);
22 if(($this->error != '') and ($this->ShowSQLError))
23 {
24 if(isset($_SERVER['REMOTE_ADDR'])) echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>');
25 echo('SQL Error: '.$this->error.' '.$Query."\n");
26 }
27
28 return($Result);
29 }
30
31 function select($Table, $What = '*', $Condition = 1)
32 {
33 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));
34 }
35
36 function delete($Table, $Condition)
37 {
38 $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
39 }
40
41 function insert($Table, $Data)
42 {
43 $Name = '';
44 $Values = '';
45 foreach($Data as $Key => $Value)
46 {
47 $Name .= ',`'.$Key.'`';
48 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"';
49 $Values .= ','.$Value;
50 }
51 $Name = substr($Name, 1);
52 $Values = substr($Values, 1);
53 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
54 }
55
56 function update($Table, $Condition, $Data)
57 {
58 $Values = '';
59 foreach($Data as $Key => $Value)
60 {
61 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"';
62 $Values .= ', `'.$Key.'`='.$Value;
63 }
64 $Values = substr($Values, 2);
65 $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
66 }
67
68 function replace($Table, $Data)
69 {
70 $Name = '';
71 $Values = '';
72 foreach($Data as $Key => $Value)
73 {
74 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"';
75 $Name .= ',`'.$Key.'`';
76 $Values .= ','.$Value;
77 }
78 $Name = substr($Name, 1);
79 $Values = substr($Values, 1);
80 //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />');
81 $this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
82 //echo($this->error().'<br>');
83 }
84
85 function charset($Charset)
86 {
87 $this->query('SET NAMES "'.$Charset.'"');
88 }
89
90 function TimeToMysqlDateTime($Time)
91 {
92 return(date('Y-m-d H:i:s', $Time));
93 }
94
95 function MysqlDateTimeToTime($Time)
96 {
97 $Parts = explode(' ', $Time);
98 $DateParts = explode('-', $Parts[0]);
99 $TimeParts = explode(':', $Parts[1]);
100 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
101 return($Result);
102 }
103
104 function MysqlDateToTime($Time)
105 {
106 return(MysqlDateTimeToTime($Time.' 0:0:0'));
107 }
108}
109
110?>
Note: See TracBrowser for help on using the repository browser.