c_mysqlDb.php
4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
require_once LIBDIR . 'errException.php';
class mysqlQueryException extends errException
{
private $query;
function __construct ($msg, $no, $file, $line, $context, $query)
{
parent::__construct ($msg, $no, $file, $line, $context);
$this->query = $query;
}
function __toString ()
{
$retStr = "Fehler in Query: \n";
$retStr .= $this->query . "\n\n";
$retStr .= $this->message . "\n\n";
$retStr .= 'Datei: ' . $this->file . "\n";
$retStr .= 'Zeile: ' . $this->line . "\n\n";
if ($this->errContext != NULL)
{
$retStr .= "Fehler Context: \n";
$retStr .= print_r ($this->errContext, TRUE) . "\n";
}
$retStr .= "Aufruf Stack: \n";
$retStr .= $this->getTraceAsString () . "\n";
return $retStr;
}
}
/***
* This class depends on errException.php, as it does not handle any
* error at all, except it catches exceptions.
*/
class c_mysqlDb
{
private $connectRes;
private $host;
private $user;
private $pass;
private $database;
private $result = array ();
private $resCount = 0;
private function getAllData ($resId, $method, $class = NULL, $parm = NULL)
{
$ret = array ();
if (isset ($this->result[$resId]) && $this->result[$resId] != FALSE)
{
try
{
mysql_data_seek ($this->result[$resId], 0);
}
catch (Exception $e)
{
if ($e->getCode () != 2)
throw ($e);
// wenn der Fehlercode gleich 2 ist war das resultset nur leer
// und ich mache weiter.
}
if ($class === NULL)
$row = $method ($this->result[$resId]);
else
$row = $method ($this->result[$resId], $class, $parm);
while ($row != FALSE)
{
$ret[] = $row;
if ($class === NULL)
$row = $method ($this->result[$resId]);
else
$row = $method ($this->result[$resId], $class, $parm);
}
}
return $ret;
}
function __construct ($host, $user, $pass, $db)
{
/* init member */
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->database = $db;
/* connect and select db */
$this->connectRes = mysql_pconnect (
$this->host,
$this->user,
$this->pass);
mysql_select_db ($this->database);
mysql_query ('set character set utf8');
}
function __destruct ()
{
/* close connection */
mysql_close ($this->connectRes);
}
function query ($query)
{
try
{
for ($i = 0; $i < $this->resCount; $i++)
if (! isset ($this->result[$i]))
break;
if ($i == $this->resCount)
$i = $this->resCount = $this->resCount + 1;
$this->result[$i] = mysql_query ($query);
if ($this->result[$i] === FALSE)
throw new Exception (mysql_error (), mysql_errno ());
return $i;
}
catch (errException $e)
{
throw new mysqlQueryException (
$e->getMessage (),
$e->getCode (),
$e->getFile (),
$e->getLine (),
$e->getErrContext (),
$query);
}
catch (Exception $e)
{
throw new mysqlQueryException (
$e->getMessage (),
$e->getCode (),
$e->getFile (),
$e->getLine (),
NULL,
$query);
}
}
function freeResult ($resId)
{
if (is_resource ($this->result[$resId]))
mysql_free_result ($this->result[$resId]);
unset ($this->result[$resId]);
}
function resetResult ($resId)
{
mysql_data_seek ($this->result[$resId], 0);
}
function getResult ($resId)
{
return $this->result[$resId];
}
function getAssoc ($resId)
{
return $this->getAllData ($resId, 'mysql_fetch_assoc');
}
function getArray ($resId)
{
return $this->getAllData ($resId, 'mysql_fetch_row');
}
function getObject ($resId, $class, $parm = NULL)
{
return $this->getAllData ($resId, 'mysql_fetch_object', $class, $parm);
}
function rows ($resId)
{
return mysql_num_rows ($this->result[$resId]);
}
function get ($resId, $row, $col)
{
if ($row > mysql_num_rows ($this->result[$resId]))
return NULL;
mysql_data_seek ($this->result[$resId], $row);
if (is_int ($col) && $col > mysql_num_fields ($this->result[$resId]))
return NULL;
if (is_string ($col))
{
$rowData = mysql_fetch_assoc ($this->result[$resId]);
if (! array_key_exists ($col, $rowData))
return NULL;
}
return mysql_result ($this->result[$resId], $row, $col);
}
// === einige nuetzlich Sachen ========================================
function setLocale ($locale)
{
$resId = $this->query ('set @LANG=\'' . $locale . '\'');
$this->freeResult ($resId);
}
};
/* quote string (und pack ' herum) */
function quoteS ($var)
{
if ($var === NULL)
return 'NULL';
return "'" . addslashes ($var) . "'";
}
?>