source: trunk/database.php@ 341

Last change on this file since 341 was 336, checked in by chronos, 13 years ago
  • Opraveno: Zobrazování chybových hlášení chyb SQL.
File size: 3.8 KB
Line 
1<?php
2
3// Extended database class
4// Date: 2011-11-25
5
6
7class 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
28class 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
53 function query($Query)
54 {
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)
61 {
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>');
69 }
70 return($Result);
71 }
72
73 function select($Table, $What = '*', $Condition = 1)
74 {
75 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));
76 }
77
78 function delete($Table, $Condition)
79 {
80 $this->PDO->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
81 }
82
83 function insert($Table, $Data)
84 {
85 $Name = '';
86 $Values = '';
87 foreach($Data as $Key => $Value)
88 {
89 $Name .= ',`'.$Key.'`';
90 if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
91 $Values .= ','.$Value;
92 }
93 $Name = substr($Name, 1);
94 $Values = substr($Values, 1);
95 $this->PDO->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
96 $this->insert_id = $this->PDO->lastInsertId();
97 }
98
99 function update($Table, $Condition, $Data)
100 {
101 $Values = '';
102 foreach($Data as $Key => $Value)
103 {
104 if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
105 $Values .= ', `'.$Key.'`='.$Value;
106 }
107 $Values = substr($Values, 2);
108 $this->PDO->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
109 }
110
111 function replace($Table, $Data)
112 {
113 $Name = '';
114 $Values = '';
115 foreach($Data as $Key => $Value)
116 {
117 if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
118 $Name .= ',`'.$Key.'`';
119 $Values .= ','.$Value;
120 }
121 $Name = substr($Name, 1);
122 $Values = substr($Values, 1);
123 //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />');
124 $this->PDO->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
125 //echo($this->error().'<br>');
126 }
127
128 function charset($Charset)
129 {
130 $this->PDO->query('SET NAMES "'.$Charset.'"');
131 }
132
133 function real_escape_string($Text)
134 {
135 return(addslashes($Text));
136 }
137
138}
139
140?>
Note: See TracBrowser for help on using the repository browser.