Changeset 712 for trunk/inc/database.php
- Timestamp:
- Jul 30, 2013, 11:34:19 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 5 5 minimanager 6 6 mmfpm 7 .settings 8 .buildpath 9 .project
-
- Property svn:ignore
-
trunk/inc/database.php
r597 r712 2 2 3 3 // Extended database class 4 // Date: 2009-02-16 5 6 class Database extends mysqli 7 { 8 var $Prefix = ''; 4 // Date: 2011-11-25 5 6 7 class DatabaseResult 8 { 9 var $PDOStatement; 10 var $num_rows = 0; 11 12 function fetch_assoc() 13 { 14 return($this->PDOStatement->fetch(PDO::FETCH_ASSOC)); 15 } 16 17 function fetch_array() 18 { 19 return($this->PDOStatement->fetch(PDO::FETCH_BOTH)); 20 } 21 22 function fetch_row() 23 { 24 return($this->PDOStatement->fetch(PDO::FETCH_NUM)); 25 } 26 } 27 28 class Database 29 { 30 var $Prefix; 31 var $Functions; 32 var $Type; 33 var $PDO; 34 var $Error; 35 var $insert_id; 36 var $LastQuery; 37 var $ShowSQLError; 38 var $ShowSQLQuery; 39 var $LogFile; 40 41 function __construct() 42 { 43 $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()'); 44 $this->Type = 'mysql'; // mysql, pgsql 45 $this->ShowSQLError = false; 46 $this->ShowSQLQuery = false; 47 $this->LogSQLQuery = false; 48 $this->LogFile = dirname(__FILE__).'/../Query.log'; 49 } 50 51 function Connect($Host, $User, $Password, $Database) 52 { 53 if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database; 54 else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host; 55 else $ConnectionString = ''; 56 $this->PDO = new PDO($ConnectionString, $User, $Password); 57 } 58 59 function select_db($Database) 60 { 61 $this->query('USE `'.$Database.'`'); 62 } 9 63 10 64 function query($Query) 11 65 { 12 global $Config; 13 14 if($Config['Web']['ShowSQLQuery'] == true) 15 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>'); 16 $Result = parent::query($Query); 17 if(($this->error != '') and ($Config['Web']['ShowSQLError'] == true)) 18 echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>'); 19 66 $this->LastQuery = $Query; 67 if($this->ShowSQLQuery == true) 68 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"); 69 if($this->LogSQLQuery == true) 70 file_put_contents($this->LogFile, $Query."\n", FILE_APPEND); 71 $Result = new DatabaseResult(); 72 $Result->PDOStatement = $this->PDO->query($Query); 73 if($Result->PDOStatement) 74 { 75 $Result->num_rows = $Result->PDOStatement->rowCount(); 76 $this->insert_id = $this->PDO->lastInsertId(); 77 } else 78 { 79 $this->Error = $this->PDO->errorInfo(); 80 $this->Error = $this->Error[2]; 81 if(($this->Error != '') and ($this->ShowSQLError == true)) 82 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); 83 throw new Exception('SQL Error: '.$this->Error); 84 } 20 85 return($Result); 21 86 } 22 87 23 88 function select($Table, $What = '*', $Condition = 1) 24 { 89 { 25 90 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition)); 26 91 } … … 37 102 foreach($Data as $Key => $Value) 38 103 { 39 $Value = strtr($Value, '"', '\"');40 104 $Name .= ',`'.$Key.'`'; 41 if($Value == 'NOW()') $Values .= ','.$Value; 42 else if($Value == 'UUID()') $Values .= ','.$Value; 43 else $Values .= ",'".$Value."'"; 105 if(!in_array($Value, $this->Functions)) 106 { 107 if(is_null($Value)) $Value = 'NULL'; 108 else $Value = $this->PDO->quote($Value); 109 } 110 $Values .= ','.$Value; 44 111 } 45 112 $Name = substr($Name, 1); 46 113 $Values = substr($Values, 1); 47 114 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 115 $this->insert_id = $this->PDO->lastInsertId(); 48 116 } 49 117 … … 53 121 foreach($Data as $Key => $Value) 54 122 { 55 $Value = strtr($Value, '"', '\"'); 56 if($Value != 'NOW()') $Value = "'".$Value."'"; 57 $Values .= ', '.$Key.'='.$Value; 123 if(!in_array($Value, $this->Functions)) 124 { 125 if(is_null($Value)) $Value = 'NULL'; 126 else $Value = $this->PDO->quote($Value); 127 } 128 $Values .= ', `'.$Key.'`='.$Value; 58 129 } 59 130 $Values = substr($Values, 2); … … 67 138 foreach($Data as $Key => $Value) 68 139 { 69 $Value = strtr($Value, '"', '\"'); 140 if(!in_array($Value, $this->Functions)) 141 { 142 if(is_null($Value)) $Value = 'NULL'; 143 else $Value = $this->PDO->quote($Value); 144 } 70 145 $Name .= ',`'.$Key.'`'; 71 if($Value == 'NOW()') $Values .= ','.$Value; 72 else $Values .= ',"'.$Value.'"'; 146 $Values .= ','.$Value; 73 147 } 74 148 $Name = substr($Name, 1); … … 83 157 $this->query('SET NAMES "'.$Charset.'"'); 84 158 } 85 } 86 87 ?> 159 160 function real_escape_string($Text) 161 { 162 return(addslashes($Text)); 163 } 164 } 165 166 function TimeToMysqlDateTime($Time) 167 { 168 if($Time == NULL) return(NULL); 169 else return(date('Y-m-d H:i:s', $Time)); 170 } 171 172 function TimeToMysqlDate($Time) 173 { 174 if($Time == NULL) return(NULL); 175 else return(date('Y-m-d', $Time)); 176 } 177 178 function TimeToMysqlTime($Time) 179 { 180 if($Time == NULL) return(NULL); 181 else return(date('H:i:s', $Time)); 182 } 183 184 function MysqlDateTimeToTime($DateTime) 185 { 186 if($DateTime == '') return(0); 187 $Parts = explode(' ', $DateTime); 188 $DateParts = explode('-', $Parts[0]); 189 $TimeParts = explode(':', $Parts[1]); 190 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 191 return($Result); 192 } 193 194 function MysqlDateToTime($Date) 195 { 196 if($Date == '') return(0); 197 return(MysqlDateTimeToTime($Date.' 0:0:0')); 198 } 199 200 function MysqlTimeToTime($Time) 201 { 202 if($Time == '') return(0); 203 return(MysqlDateTimeToTime('0000-00-00 '.$Time)); 204 }
Note:
See TracChangeset
for help on using the changeset viewer.