1 | <?php
|
---|
2 |
|
---|
3 | // Extended database class
|
---|
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());
|
---|
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
|
---|
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 | var $Database = '';
|
---|
40 |
|
---|
41 | function __construct($Host, $User, $Password, $Database)
|
---|
42 | {
|
---|
43 | $this->Database = $Database;
|
---|
44 | if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
|
---|
45 | else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
|
---|
46 | else $ConnectionString = '';
|
---|
47 | $this->PDO = new PDO($ConnectionString, $User, $Password);
|
---|
48 | $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
---|
49 | }
|
---|
50 |
|
---|
51 | function select_db($Database)
|
---|
52 | {
|
---|
53 | $this->Database = $Database;
|
---|
54 | $this->query('USE '.$Database);
|
---|
55 | }
|
---|
56 |
|
---|
57 | function query($Query)
|
---|
58 | {
|
---|
59 | try
|
---|
60 | {
|
---|
61 | $this->LastQuery = $Query;
|
---|
62 | if($this->ShowSQLQuery == true)
|
---|
63 | 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");
|
---|
64 | $Result = new DatabaseResult();
|
---|
65 | $Result->PDOStatement = $this->PDO->query($Query);
|
---|
66 | if($Result->PDOStatement)
|
---|
67 | $Result->num_rows = $Result->PDOStatement->rowCount();
|
---|
68 | } catch(PDOException $E)
|
---|
69 | {
|
---|
70 | $this->Error = $E->getMessage();
|
---|
71 | if(($this->Error != '') and ($this->ShowSQLError == true))
|
---|
72 | echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>');
|
---|
73 | }
|
---|
74 | return($Result);
|
---|
75 | }
|
---|
76 |
|
---|
77 | function select($Table, $What = '*', $Condition = 1)
|
---|
78 | {
|
---|
79 | return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));
|
---|
80 | }
|
---|
81 |
|
---|
82 | function delete($Table, $Condition)
|
---|
83 | {
|
---|
84 | $this->PDO->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
|
---|
85 | }
|
---|
86 |
|
---|
87 | function insert($Table, $Data)
|
---|
88 | {
|
---|
89 | $Name = '';
|
---|
90 | $Values = '';
|
---|
91 | foreach($Data as $Key => $Value)
|
---|
92 | {
|
---|
93 | $Name .= ',`'.$Key.'`';
|
---|
94 | if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
|
---|
95 | $Values .= ','.$Value;
|
---|
96 | }
|
---|
97 | $Name = substr($Name, 1);
|
---|
98 | $Values = substr($Values, 1);
|
---|
99 | $this->PDO->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
|
---|
100 | $this->insert_id = $this->PDO->lastInsertId();
|
---|
101 | }
|
---|
102 |
|
---|
103 | function update($Table, $Condition, $Data)
|
---|
104 | {
|
---|
105 | $Values = '';
|
---|
106 | foreach($Data as $Key => $Value)
|
---|
107 | {
|
---|
108 | if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
|
---|
109 | $Values .= ', `'.$Key.'`='.$Value;
|
---|
110 | }
|
---|
111 | $Values = substr($Values, 2);
|
---|
112 | $this->PDO->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
|
---|
113 | }
|
---|
114 |
|
---|
115 | function replace($Table, $Data)
|
---|
116 | {
|
---|
117 | $Name = '';
|
---|
118 | $Values = '';
|
---|
119 | foreach($Data as $Key => $Value)
|
---|
120 | {
|
---|
121 | if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
|
---|
122 | $Name .= ',`'.$Key.'`';
|
---|
123 | $Values .= ','.$Value;
|
---|
124 | }
|
---|
125 | $Name = substr($Name, 1);
|
---|
126 | $Values = substr($Values, 1);
|
---|
127 | //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />');
|
---|
128 | $this->PDO->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
|
---|
129 | //echo($this->error().'<br>');
|
---|
130 | }
|
---|
131 |
|
---|
132 | function charset($Charset)
|
---|
133 | {
|
---|
134 | $this->PDO->query('SET NAMES "'.$Charset.'"');
|
---|
135 | }
|
---|
136 |
|
---|
137 | function real_escape_string($Text)
|
---|
138 | {
|
---|
139 | return(addslashes($Text));
|
---|
140 | }
|
---|
141 |
|
---|
142 | }
|
---|
143 |
|
---|
144 | ?>
|
---|