Changeset 800 for trunk/includes/Database.php
- Timestamp:
- Mar 16, 2014, 11:15:43 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/includes/Database.php
r770 r800 8 8 var $PDOStatement; 9 9 var $num_rows = 0; 10 10 11 11 function fetch_assoc() 12 12 { 13 13 return($this->PDOStatement->fetch(PDO::FETCH_ASSOC)); 14 14 } 15 15 16 16 function fetch_array() 17 17 { … … 29 29 var $Prefix; 30 30 var $Functions; 31 var $Type; 31 var $Type; 32 32 var $PDO; 33 33 var $Error; … … 37 37 var $ShowSQLQuery; 38 38 var $LogFile; 39 39 40 40 function __construct() 41 { 41 { 42 42 $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()'); 43 43 $this->Type = 'mysql'; // mysql, pgsql … … 47 47 $this->LogFile = dirname(__FILE__).'/../Query.log'; 48 48 } 49 49 50 50 function Connect($Host, $User, $Password, $Database) 51 { 51 { 52 52 if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database; 53 53 else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host; … … 55 55 $this->PDO = new PDO($ConnectionString, $User, $Password); 56 56 } 57 57 58 58 function select_db($Database) 59 59 { 60 60 $this->query('USE `'.$Database.'`'); 61 61 } 62 62 63 63 function query($Query) 64 64 { … … 67 67 $Result = new DatabaseResult(); 68 68 $Result->PDOStatement = $this->PDO->query($Query); 69 if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) 70 $Duration = ' ; '.round(microtime() - $QueryStartTime, 3). ' s';69 if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) 70 $Duration = ' ; '.round(microtime() - $QueryStartTime, 4). ' s'; 71 71 if($this->LogSQLQuery == true) 72 72 file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND); … … 81 81 $this->Error = $this->PDO->errorInfo(); 82 82 $this->Error = $this->Error[2]; 83 if(($this->Error != '') and ($this->ShowSQLError == true)) 83 if(($this->Error != '') and ($this->ShowSQLError == true)) 84 84 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); 85 85 throw new Exception('SQL Error: '.$this->Error); 86 86 } 87 return($Result); 87 return($Result); 88 88 } 89 89 90 90 function select($Table, $What = '*', $Condition = 1) 91 { 92 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition)); 91 { 92 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition)); 93 93 } 94 94 95 95 function delete($Table, $Condition) 96 96 { 97 $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 98 } 99 97 $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 98 } 99 100 100 function insert($Table, $Data) 101 101 { … … 105 105 { 106 106 $Name .= ',`'.$Key.'`'; 107 if(!in_array($Value, $this->Functions)) 107 if(!in_array($Value, $this->Functions)) 108 108 { 109 109 if(is_null($Value)) $Value = 'NULL'; … … 114 114 $Name = substr($Name, 1); 115 115 $Values = substr($Values, 1); 116 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 116 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 117 117 $this->insert_id = $this->PDO->lastInsertId(); 118 118 } 119 119 120 120 function update($Table, $Condition, $Data) 121 121 { … … 123 123 foreach($Data as $Key => $Value) 124 124 { 125 if(!in_array($Value, $this->Functions)) 125 if(!in_array($Value, $this->Functions)) 126 126 { 127 127 if(is_null($Value)) $Value = 'NULL'; … … 130 130 $Values .= ', `'.$Key.'`='.$Value; 131 131 } 132 $Values = substr($Values, 2); 132 $Values = substr($Values, 2); 133 133 $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')'); 134 134 } 135 135 136 136 function replace($Table, $Data) 137 137 { … … 140 140 foreach($Data as $Key => $Value) 141 141 { 142 if(!in_array($Value, $this->Functions)) 142 if(!in_array($Value, $this->Functions)) 143 143 { 144 144 if(is_null($Value)) $Value = 'NULL'; … … 154 154 //echo($this->error().'<br>'); 155 155 } 156 156 157 157 function charset($Charset) 158 158 { 159 159 $this->query('SET NAMES "'.$Charset.'"'); 160 160 } 161 161 162 162 function real_escape_string($Text) 163 163 { 164 164 return(addslashes($Text)); 165 165 } 166 166 167 167 function quote($Text) 168 168 { 169 169 return($this->PDO->quote($Text)); 170 170 } 171 171 172 172 function __sleep() 173 173 { … … 177 177 function __wakeup() 178 178 { 179 } 179 } 180 180 } 181 181 … … 183 183 { 184 184 if($Time == NULL) return(NULL); 185 else return(date('Y-m-d H:i:s', $Time)); 185 else return(date('Y-m-d H:i:s', $Time)); 186 186 } 187 187 … … 189 189 { 190 190 if($Time == NULL) return(NULL); 191 else return(date('Y-m-d', $Time)); 191 else return(date('Y-m-d', $Time)); 192 192 } 193 193 … … 195 195 { 196 196 if($Time == NULL) return(NULL); 197 else return(date('H:i:s', $Time)); 197 else return(date('H:i:s', $Time)); 198 198 } 199 199 200 200 function MysqlDateTimeToTime($DateTime) 201 201 { 202 if($DateTime == '') return(0); 202 if($DateTime == '') return(0); 203 203 $Parts = explode(' ', $DateTime); 204 $DateParts = explode('-', $Parts[0]); 204 $DateParts = explode('-', $Parts[0]); 205 205 $TimeParts = explode(':', $Parts[1]); 206 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 207 return($Result); 206 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 207 return($Result); 208 208 } 209 209 … … 211 211 { 212 212 if($Date == '') return(0); 213 return(MysqlDateTimeToTime($Date.' 0:0:0')); 213 return(MysqlDateTimeToTime($Date.' 0:0:0')); 214 214 } 215 215 … … 217 217 { 218 218 if($Time == '') return(0); 219 return(MysqlDateTimeToTime('0000-00-00 '.$Time)); 220 } 219 return(MysqlDateTimeToTime('0000-00-00 '.$Time)); 220 }
Note:
See TracChangeset
for help on using the changeset viewer.