|
1 <?php |
|
2 //include_once("configy.inc.php"); |
|
3 /** |
|
4 * dbconnection |
|
5 * |
|
6 * @package fhiiqm |
|
7 * @author IQM achilleus |
|
8 * @copyright Bettina Schwarzer |
|
9 * @version 05/2011 |
|
10 * @access public |
|
11 */ |
|
12 class dbconnection |
|
13 { protected $_server; |
|
14 protected $_user; |
|
15 protected $_passwort; |
|
16 protected $_dbname; |
|
17 var $_dbtyp; |
|
18 |
|
19 protected $_connid; |
|
20 protected $_statement; |
|
21 var $error; |
|
22 var $_lastmessage; |
|
23 |
|
24 public $result; |
|
25 |
|
26 function __construct() |
|
27 { |
|
28 include_once("conf.inc.php"); |
|
29 // global $config; |
|
30 |
|
31 $this->_server = ( isset($server) && $server ) ? $server : $config["server"]; |
|
32 $this->_user = ( isset($user) && $user ) ? $user : $config["user"]; |
|
33 $this->_passwort = ( isset($passwort) && $passwort ) ? $passwort : $config["passwort"]; |
|
34 $this->_dbname = ( isset($dbname) && $dbname ) ? $dbname : $config["dbname"]; |
|
35 |
|
36 $this->_connid = false; |
|
37 $this->error = ""; |
|
38 $this->_lastmessage = ""; |
|
39 // $this -> _connid = @new mysqli($this->_server, $this->_user, $this->_passwort, $this->_dbname); |
|
40 $this->_connid = mysqli_init(); |
|
41 $this->_connid->real_connect($this->_server, $this->_user, $this->_passwort, $this->_dbname); |
|
42 // $this->_connid->set_charset("utf8"); |
|
43 // echo $this-> _connid->character_set_name()."<br>\n"; |
|
44 // $this -> _connid -> set_charset('latin1'); //latin1_swedish_ci, ist Standard |
|
45 // echo $this-> _connid->character_set_name()."<br>\n"; |
|
46 |
|
47 if (mysqli_connect_errno()) |
|
48 { |
|
49 $this->error = mysqli_connect_error(); |
|
50 $this -> _connid = false; |
|
51 // printf("keine Verbindung zu %s, Fehler: %s", $config["dbname"], $this->error); |
|
52 exit(); |
|
53 } |
|
54 // printf("Verbindung zu %s steht<br>\n", $config["dbname"]); |
|
55 // echo "Verbindung zu " . $config["dbname"] . " steht.<br>\n"; |
|
56 } |
|
57 |
|
58 function __destruct() |
|
59 { |
|
60 $this -> close(); |
|
61 } |
|
62 |
|
63 function close() |
|
64 { |
|
65 if ($this -> _connid) |
|
66 { |
|
67 $this -> _connid -> close(); |
|
68 $this -> _connid = false; |
|
69 } |
|
70 $this->error = ""; |
|
71 |
|
72 } |
|
73 |
|
74 function queryObjectArray($sql) |
|
75 {// in $result_arr steht Array des Ergebnisses als Objekt |
|
76 if ( is_string($sql) ) |
|
77 { |
|
78 if ($this->result = $this->_connid->query($sql)) |
|
79 { |
|
80 if ($this->result->num_rows) |
|
81 { |
|
82 while ($row = $this->result->fetch_object()) |
|
83 $result_arr[] = $row; |
|
84 $this->result->free(); |
|
85 $this->_connid->next_result(); // wegen OUT-Parameter in stored procedures |
|
86 return $result_arr; |
|
87 } |
|
88 else |
|
89 { |
|
90 $this->error = ""; |
|
91 return false; |
|
92 } |
|
93 } |
|
94 else |
|
95 { |
|
96 $this->error = $this->_connid->error; |
|
97 return false; |
|
98 } |
|
99 } |
|
100 else |
|
101 { |
|
102 $this->error = "'$sql'<br> ist kein SQL-Statement!"; |
|
103 return false; |
|
104 } |
|
105 |
|
106 } |
|
107 |
|
108 function querySingleItem($sql) |
|
109 {// gibt nur 1 Zeile mit einer Spalte zurueck, z.B. Count() |
|
110 if ($this->result = $this->_connid->query($sql)) |
|
111 { |
|
112 if ($row = $this->result->fetch_array()) |
|
113 { |
|
114 $this->result->close(); |
|
115 return $row[0]; |
|
116 } |
|
117 else return false; |
|
118 } |
|
119 else |
|
120 { |
|
121 $this->error = $this->_connid->error; |
|
122 return false; |
|
123 |
|
124 } |
|
125 } |
|
126 |
|
127 function execute($sql) |
|
128 {// INSERT, UPDATE, DELETE |
|
129 if ($this->_connid->real_query($sql)) |
|
130 return true; |
|
131 else |
|
132 { |
|
133 $this->error = $this->_connid->errno . ": " . $this->_connid->error; |
|
134 return false; |
|
135 } |
|
136 } |
|
137 |
|
138 function insertId() |
|
139 {// liefert letzten Autowert bei INSERT |
|
140 return $this->_connid->insert_id; |
|
141 } |
|
142 |
|
143 function numrows($sql) |
|
144 {// Anzahl der Ergebniszeilen |
|
145 if ($this->result = $this->_connid->query($sql)) |
|
146 { |
|
147 return $this->result -> num_rows; |
|
148 } |
|
149 else |
|
150 return 0; |
|
151 } |
|
152 |
|
153 function setcharset($string) |
|
154 {// Zeichensatz fuer Client-Verbindung festlegen |
|
155 if ($this -> _connid) $this ->_connid -> set_charset($string); |
|
156 } |
|
157 |
|
158 function escapestring($string) |
|
159 {// string escapen: '," ersetzen durch \',\" |
|
160 if ($this -> _connid) return $this ->_connid -> real_escape_string($string); |
|
161 } |
|
162 |
|
163 function getclient_info() |
|
164 {// MySQL-Information |
|
165 if ($this -> _connid) return $this ->_connid -> get_client_info(); |
|
166 } |
|
167 |
|
168 function gethost_info() |
|
169 {// Typ der Verbindung zur DB |
|
170 if ($this -> _connid) return $this ->_connid -> get_server_info(); |
|
171 } |
|
172 |
|
173 function stmtinit() |
|
174 {// $_statement wird als Objekt der mysqli-Klasse erstellt, damit simd alle Methoden der MySQLi_STMT class verfuegbar |
|
175 if ($this -> _connid) return $this->_statement = $this->_connid->stmt_init(); |
|
176 } |
|
177 } |
|
178 |
|
179 /** |
|
180 * DateTime_s |
|
181 * |
|
182 * @package fhiiqm |
|
183 * @author IQM achilleus |
|
184 * @copyright Bettina Schwarzer |
|
185 * @version 07/2011 |
|
186 * @access public |
|
187 */ |
|
188 class DateTime_s extends DateTime |
|
189 { |
|
190 public function __toString() |
|
191 { // Datetime -> String fuer INSERT/UPDATE |
|
192 return $this->format('Y-m-d H:i:s'); |
|
193 } |
|
194 } |
|
195 ?> |