Changeset 507 for trunk/includes/Database.php
- Timestamp:
- Feb 15, 2013, 10:12:08 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/includes/Database.php
r457 r507 2 2 3 3 // Extended database class 4 // Date: 201 0-01-294 // Date: 2011-11-25 5 5 6 class Database extends mysqli 6 7 class DatabaseResult 7 8 { 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 17 function open() 9 var $PDOStatement; 10 var $num_rows = 0; 11 12 function fetch_assoc() 18 13 { 19 parent::connect($this->HostName, $this->UserName, $this->Password, $this->Schema); 20 $this->charset($this->Charset); 14 return($this->PDOStatement->fetch(PDO::FETCH_ASSOC)); 15 } 16 17 function fetch_array() 18 { 19 return($this->PDOStatement->fetch(PDO::FETCH_BOTH)); 21 20 } 22 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 = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()'); 32 var $Type = 'mysql'; // mysql, pgsql 33 var $PDO; 34 var $Error = ''; 35 var $insert_id; 36 var $LastQuery = ''; 37 var $ShowSQLError = false; 38 var $ShowSQLQuery = false; 39 40 function __construct($Host, $User, $Password, $Database) 41 { 42 if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database; 43 else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host; 44 else $ConnectionString = ''; 45 $this->PDO = new PDO($ConnectionString, $User, $Password); 46 } 47 48 function select_db($Database) 49 { 50 $this->query('USE `'.$Database.'`'); 51 } 52 23 53 function query($Query) 24 54 { 25 26 if($this->ShowSQLQuery) 55 $this->LastQuery = $Query; 56 if($this->ShowSQLQuery == true) 57 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"); 58 $Result = new DatabaseResult(); 59 $Result->PDOStatement = $this->PDO->query($Query); 60 if($Result->PDOStatement) 27 61 { 28 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>'); 29 else echo($Query."\n"); 62 $Result->num_rows = $Result->PDOStatement->rowCount(); 63 } else 64 { 65 $this->Error = $this->PDO->errorInfo(); 66 $this->Error = $this->Error[2]; 67 if(($this->Error != '') and ($this->ShowSQLError == true)) 68 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); 30 69 } 31 $Result = parent::query($Query); 32 if(($this->error != '') and ($this->ShowSQLError)) 33 { 34 if(isset($_SERVER['REMOTE_ADDR'])) echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>'); 35 echo('SQL Error: '.$this->error.' '.$Query."\n"); 36 } 37 70 $this->insert_id = $this->PDO->lastInsertId(); 38 71 return($Result); 39 72 } 40 73 41 74 function select($Table, $What = '*', $Condition = 1) 42 { 75 { 43 76 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition)); 44 77 } … … 56 89 { 57 90 $Name .= ',`'.$Key.'`'; 91 if(!in_array($Value, $this->Functions)) 92 { 93 if(is_null($Value)) $Value = 'NULL'; 94 else $Value = $this->PDO->quote($Value); 95 } 58 96 $Values .= ','.$Value; 59 97 } … … 61 99 $Values = substr($Values, 1); 62 100 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 101 $this->insert_id = $this->PDO->lastInsertId(); 63 102 } 64 103 … … 68 107 foreach($Data as $Key => $Value) 69 108 { 109 if(!in_array($Value, $this->Functions)) 110 { 111 if(is_null($Value)) $Value = 'NULL'; 112 else $Value = $this->PDO->quote($Value); 113 } 70 114 $Values .= ', `'.$Key.'`='.$Value; 71 115 } … … 80 124 foreach($Data as $Key => $Value) 81 125 { 126 if(!in_array($Value, $this->Functions)) 127 { 128 if(is_null($Value)) $Value = 'NULL'; 129 else $Value = $this->PDO->quote($Value); 130 } 82 131 $Name .= ',`'.$Key.'`'; 83 132 $Values .= ','.$Value; … … 94 143 $this->query('SET NAMES "'.$Charset.'"'); 95 144 } 145 146 function real_escape_string($Text) 147 { 148 return(addslashes($Text)); 149 } 150 151 } 96 152 97 function TimeToMysqlDateTime($Time) 98 { 99 return(date('Y-m-d H:i:s', $Time)); 100 } 153 function TimeToMysqlDateTime($Time) 154 { 155 if($Time == NULL) return(NULL); 156 else return(date('Y-m-d H:i:s', $Time)); 157 } 101 158 102 function MysqlDateTimeToTime($Time) 103 { 104 $Parts = explode(' ', $Time); 105 $DateParts = explode('-', $Parts[0]); 106 $TimeParts = explode(':', $Parts[1]); 107 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 108 return($Result); 109 } 159 function TimeToMysqlDate($Time) 160 { 161 if($Time == NULL) return(NULL); 162 else return(date('Y-m-d', $Time)); 163 } 110 164 111 function MysqlDateToTime($Time) 112 { 113 return($this->MysqlDateTimeToTime($Time.' 0:0:0')); 114 } 165 function TimeToMysqlTime($Time) 166 { 167 if($Time == NULL) return(NULL); 168 else return(date('H:i:s', $Time)); 169 } 170 171 function MysqlDateTimeToTime($DateTime) 172 { 173 if($DateTime == '') return(0); 174 $Parts = explode(' ', $DateTime); 175 $DateParts = explode('-', $Parts[0]); 176 $TimeParts = explode(':', $Parts[1]); 177 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 178 return($Result); 179 } 180 181 function MysqlDateToTime($Date) 182 { 183 if($Date == '') return(0); 184 return(MysqlDateTimeToTime($Date.' 0:0:0')); 185 } 186 187 function MysqlTimeToTime($Time) 188 { 189 if($Time == '') return(0); 190 return(MysqlDateTimeToTime('0000-00-00 '.$Time)); 115 191 } 116 192
Note:
See TracChangeset
for help on using the changeset viewer.