source: trunk/class/Database.php

Last change on this file was 10, checked in by maron, 13 years ago

připojení do databáze

  • Property svn:executable set to *
File size: 3.5 KB
Line 
1<?php
2
3// Extended database class
4// Date: 2012-06-12
5
6class Database extends mysqli
7{
8 var $HostName = 'localhost';
9 var $UserName;
10 var $Password;
11 var $Schema;
12 var $Charset = 'utf8';
13 var $Prefix = '';
14 var $ShowSQLQuery = false;
15 var $ShowSQLError = false;
16 var $Config;
17
18 function __construct()
19 {
20 $this->Config = new Config();
21 $this->HostName = $this->Config->Database['Host'];
22 $this->UserName = $this->Config->Database['User'];
23 $this->Password = $this->Config->Database['Password'];
24 $this->Schema = $this->Config->Database['Database'];
25 $this->Charset = $this->Config->Database['Charset'];
26 $this->ShowSQLQuery = $this->Config->Web['ShowSQLQuery'];
27 $this->ShowSQLError = $this->Config->Web['ShowSQLError'];
28 $this->open();
29
30 }
31
32 function open()
33 {
34 parent::connect($this->HostName, $this->UserName, $this->Password, $this->Schema);
35 $this->charset($this->Charset);
36 }
37
38 function query($Query)
39 {
40 if($this->ShowSQLQuery)
41 {
42 echo $this->ShowSQLQuery;
43 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>');
44 else echo($Query."\n");
45 }
46 $Result = parent::query($Query);
47 if(($this->error != '') and ($this->ShowSQLError))
48 {
49 if(isset($_SERVER['REMOTE_ADDR'])) echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>');
50 echo('SQL Error: '.$this->error.' '.$Query."\n");
51 }
52
53 return($Result);
54 }
55
56 function select($Table, $What = '*', $Condition = 1)
57 {
58 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));
59 }
60
61 function delete($Table, $Condition)
62 {
63 $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
64 }
65
66 function insert($Table, $Data)
67 {
68 $Name = '';
69 $Values = '';
70 foreach($Data as $Key => $Value)
71 {
72 $Name .= ',`'.$Key.'`';
73 $Values .= ','.$Value;
74 }
75 $Name = substr($Name, 1);
76 $Values = substr($Values, 1);
77 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
78 }
79
80 function update($Table, $Condition, $Data)
81 {
82 $Values = '';
83 foreach($Data as $Key => $Value)
84 {
85 $Values .= ', `'.$Key.'`='.$Value;
86 }
87 $Values = substr($Values, 2);
88 $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
89 }
90
91 function replace($Table, $Data)
92 {
93 $Name = '';
94 $Values = '';
95 foreach($Data as $Key => $Value)
96 {
97 $Name .= ',`'.$Key.'`';
98 $Values .= ','.$Value;
99 }
100 $Name = substr($Name, 1);
101 $Values = substr($Values, 1);
102 //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />');
103 $this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
104 //echo($this->error().'<br>');
105 }
106
107 function charset($Charset)
108 {
109 $this->query('SET NAMES "'.$Charset.'"');
110 }
111
112 function TimeToMysqlDateTime($Time)
113 {
114 return(date('Y-m-d H:i:s', $Time));
115 }
116
117 function MysqlDateTimeToTime($Time)
118 {
119 $Parts = explode(' ', $Time);
120 $DateParts = explode('-', $Parts[0]);
121 $TimeParts = explode(':', $Parts[1]);
122 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
123 return($Result);
124 }
125
126 function MysqlDateToTime($Time)
127 {
128 return($this->MysqlDateTimeToTime($Time.' 0:0:0'));
129 }
130}
131
132?>
Note: See TracBrowser for help on using the repository browser.