Changeset 659 for trunk/Common/Database.php
- Timestamp:
- May 29, 2014, 11:59:39 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/Database.php
r634 r659 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 { … … 36 36 var $ShowSQLError; 37 37 var $ShowSQLQuery; 38 38 39 39 function __construct() 40 { 40 { 41 41 $this->Type = 'mysql'; // mysql, pgsql 42 42 $this->ShowSQLError = false; … … 44 44 $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()'); 45 45 } 46 46 47 47 function Connect($Host, $User, $Password, $Database) 48 { 48 { 49 49 if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database; 50 50 else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host; … … 59 59 } 60 60 } 61 61 62 62 function Disconnect() 63 63 { 64 64 unset($this->PDO); 65 65 } 66 66 67 67 function Connected() 68 68 { 69 69 return(isset($this->PDO)); 70 70 } 71 71 72 72 function select_db($Database) 73 73 { 74 74 $this->query('USE `'.$Database.'`'); 75 75 } 76 76 77 77 function query($Query) 78 78 { 79 79 if(!$this->Connected()) throw new Exception('Not connected to database'); 80 80 $this->LastQuery = $Query; 81 if($this->ShowSQLQuery == true) 81 if($this->ShowSQLQuery == true) 82 82 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>'."\n"); 83 83 $Result = new DatabaseResult(); … … 91 91 $this->Error = $this->PDO->errorInfo(); 92 92 $this->Error = $this->Error[2]; 93 if(($this->Error != '') and ($this->ShowSQLError == true)) 93 if(($this->Error != '') and ($this->ShowSQLError == true)) 94 94 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); 95 95 throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query); 96 96 } 97 return($Result); 97 return($Result); 98 98 } 99 99 100 100 function select($Table, $What = '*', $Condition = 1) 101 { 102 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition)); 101 { 102 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition)); 103 103 } 104 104 105 105 function delete($Table, $Condition) 106 106 { 107 $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 108 } 109 107 $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 108 } 109 110 110 function insert($Table, $Data) 111 111 { … … 115 115 { 116 116 $Name .= ',`'.$Key.'`'; 117 if(!in_array($Value, $this->Functions)) 117 if(!in_array($Value, $this->Functions)) 118 118 { 119 119 if(is_null($Value)) $Value = 'NULL'; … … 124 124 $Name = substr($Name, 1); 125 125 $Values = substr($Values, 1); 126 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 126 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 127 127 $this->insert_id = $this->PDO->lastInsertId(); 128 128 } 129 129 130 130 function update($Table, $Condition, $Data) 131 131 { … … 133 133 foreach($Data as $Key => $Value) 134 134 { 135 if(!in_array($Value, $this->Functions)) 135 if(!in_array($Value, $this->Functions)) 136 136 { 137 137 if(is_null($Value)) $Value = 'NULL'; … … 140 140 $Values .= ', `'.$Key.'`='.$Value; 141 141 } 142 $Values = substr($Values, 2); 142 $Values = substr($Values, 2); 143 143 $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')'); 144 144 } 145 145 146 146 function replace($Table, $Data) 147 147 { … … 150 150 foreach($Data as $Key => $Value) 151 151 { 152 if(!in_array($Value, $this->Functions)) 152 if(!in_array($Value, $this->Functions)) 153 153 { 154 154 if(is_null($Value)) $Value = 'NULL'; … … 164 164 //echo($this->error().'<br>'); 165 165 } 166 166 167 167 function charset($Charset) 168 168 { 169 169 $this->query('SET NAMES "'.$Charset.'"'); 170 170 } 171 171 172 172 function real_escape_string($Text) 173 173 { 174 174 return(addslashes($Text)); 175 175 } 176 176 177 177 public function __sleep() 178 178 { … … 188 188 { 189 189 if($Time == NULL) return(NULL); 190 else return(date('Y-m-d H:i:s', $Time)); 190 else return(date('Y-m-d H:i:s', $Time)); 191 191 } 192 192 … … 194 194 { 195 195 if($Time == NULL) return(NULL); 196 else return(date('Y-m-d', $Time)); 196 else return(date('Y-m-d', $Time)); 197 197 } 198 198 … … 200 200 { 201 201 if($Time == NULL) return(NULL); 202 else return(date('H:i:s', $Time)); 202 else return(date('H:i:s', $Time)); 203 203 } 204 204 205 205 function MysqlDateTimeToTime($DateTime) 206 206 { 207 if($DateTime == '') return(NULL); 207 if($DateTime == '') return(NULL); 208 208 $Parts = explode(' ', $DateTime); 209 $DateParts = explode('-', $Parts[0]); 209 $DateParts = explode('-', $Parts[0]); 210 210 $TimeParts = explode(':', $Parts[1]); 211 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 212 return($Result); 211 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 212 return($Result); 213 213 } 214 214 … … 216 216 { 217 217 if($Date == '') return(NULL); 218 return(MysqlDateTimeToTime($Date.' 0:0:0')); 218 return(MysqlDateTimeToTime($Date.' 0:0:0')); 219 219 } 220 220 … … 222 222 { 223 223 if($Time == '') return(NULL); 224 return(MysqlDateTimeToTime('0000-00-00 '.$Time)); 225 } 224 return(MysqlDateTimeToTime('0000-00-00 '.$Time)); 225 }
Note:
See TracChangeset
for help on using the changeset viewer.