Changeset 319
- Timestamp:
- Nov 27, 2011, 2:26:59 PM (13 years ago)
- Location:
- trunk
- Files:
-
- 5 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/database.php
r292 r319 2 2 3 3 // Extended database class 4 // Date: 20 09-02-164 // Date: 2011-11-25 5 5 6 6 7 class Database extends mysqli 7 class DatabaseResult 8 { 9 var $PDOStatement; 10 var $num_rows = 0; 11 12 function fetch_assoc() 13 { 14 return($this->PDOStatement->fetch()); 15 } 16 17 function fetch_array() 18 { 19 return($this->PDOStatement->fetch()); 20 } 21 22 function fetch_row() 23 { 24 return($this->PDOStatement->fetch()); 25 } 26 } 27 28 class Database 8 29 { 9 30 var $Prefix = ''; 10 31 var $Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()'); 32 var $Type = 'mysql'; // mysql, pgsql 33 var $PDO; 34 var $Error = ''; 35 36 function __construct($Host, $User, $Password, $Database) 37 { 38 if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database; 39 else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host; 40 else $ConnectionString = ''; 41 $this->PDO = new PDO($ConnectionString, $User, $Password); 42 } 43 44 function select_db($Database) 45 { 46 $this->query('USE '.$Database); 47 } 11 48 12 49 function query($Query) 13 50 { 14 51 global $Config; 15 52 16 if($Config['Web']['ShowSQLQuery'] == true) 17 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"); 18 $Result = parent::query($Query); 19 if(($this->error != '') and ($Config['Web']['ShowSQLError'] == true)) 20 echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>'); 53 if($Config['Web']['ShowSQLQuery'] == true) 54 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"); 55 $Result = new DatabaseResult(); 56 $Result->PDOStatement = $this->PDO->query($Query); 57 $Result->num_rows = $Result->PDOStatement->rowCount(); 58 if(($this->Error != '') and ($Config['Web']['ShowSQLError'] == true)) 59 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); 21 60 22 61 return($Result); … … 24 63 25 64 function select($Table, $What = '*', $Condition = 1) 26 { 65 { 27 66 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition)); 28 67 } … … 30 69 function delete($Table, $Condition) 31 70 { 32 $this-> query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);71 $this->PDO->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 33 72 } 34 73 … … 40 79 { 41 80 $Name .= ',`'.$Key.'`'; 42 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"';81 if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value); 43 82 $Values .= ','.$Value; 44 83 } 45 84 $Name = substr($Name, 1); 46 85 $Values = substr($Values, 1); 47 $this-> query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');86 $this->PDO->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 48 87 } 49 88 … … 53 92 foreach($Data as $Key => $Value) 54 93 { 55 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"';94 if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value); 56 95 $Values .= ', `'.$Key.'`='.$Value; 57 96 } 58 97 $Values = substr($Values, 2); 59 $this-> query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');98 $this->PDO->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')'); 60 99 } 61 100 … … 66 105 foreach($Data as $Key => $Value) 67 106 { 68 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"';107 if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value); 69 108 $Name .= ',`'.$Key.'`'; 70 109 $Values .= ','.$Value; … … 73 112 $Values = substr($Values, 1); 74 113 //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />'); 75 $this-> query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');114 $this->PDO->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 76 115 //echo($this->error().'<br>'); 77 116 } … … 79 118 function charset($Charset) 80 119 { 81 $this-> query('SET NAMES "'.$Charset.'"');120 $this->PDO->query('SET NAMES "'.$Charset.'"'); 82 121 } 122 83 123 } 84 124
Note:
See TracChangeset
for help on using the changeset viewer.