"Fossies" - the Fresh Open Source Software Archive 
Member "mythreads/lib/phplib/db_mysql.inc" (29 Mar 2001, 9972 Bytes) of package /linux/privat/mythreads-links_1.2.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) fasm source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "db_mysql.inc" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
1.2.0_vs_1.2.1.
1 <?php
2 /*
3 * Session Management for PHP3
4 *
5 * Copyright (c) 1998-2000 NetUSE AG
6 * Boris Erdmann, Kristian Koehntopp
7 *
8 * $Id: db_mysql.inc,v 1.1 2001-03-29 21:51:52 ldrolez Exp $
9 *
10 */
11
12 class DB_Sql {
13
14 /* public: connection parameters */
15 var $Host = "";
16 var $Database = "";
17 var $User = "";
18 var $Password = "";
19
20 /* public: configuration parameters */
21 var $Auto_Free = 0; ## Set to 1 for automatic mysql_free_result()
22 var $Debug = 0; ## Set to 1 for debugging messages.
23 var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
24 var $Seq_Table = "db_sequence";
25
26 /* public: result array and current row number */
27 var $Record = array();
28 var $Row;
29
30 /* public: current error number and error text */
31 var $Errno = 0;
32 var $Error = "";
33
34 /* public: this is an api revision, not a CVS revision. */
35 var $type = "mysql";
36 var $revision = "1.2";
37
38 /* private: link and query handles */
39 var $Link_ID = 0;
40 var $Query_ID = 0;
41
42
43
44 /* public: constructor */
45 function DB_Sql($query = "") {
46 $this->query($query);
47 }
48
49 /* public: some trivial reporting */
50 function link_id() {
51 return $this->Link_ID;
52 }
53
54 function query_id() {
55 return $this->Query_ID;
56 }
57
58 /* public: connection management */
59 function connect($Database = "", $Host = "", $User = "", $Password = "") {
60 /* Handle defaults */
61 if ("" == $Database)
62 $Database = $this->Database;
63 if ("" == $Host)
64 $Host = $this->Host;
65 if ("" == $User)
66 $User = $this->User;
67 if ("" == $Password)
68 $Password = $this->Password;
69
70 /* establish connection, select database */
71 if ( 0 == $this->Link_ID ) {
72
73 $this->Link_ID=mysql_pconnect($Host, $User, $Password);
74 if (!$this->Link_ID) {
75 $this->halt("pconnect($Host, $User, \$Password) failed.");
76 return 0;
77 }
78
79 if (!@mysql_select_db($Database,$this->Link_ID)) {
80 $this->halt("cannot use database ".$this->Database);
81 return 0;
82 }
83 }
84
85 return $this->Link_ID;
86 }
87
88 /* public: discard the query result */
89 function free() {
90 @mysql_free_result($this->Query_ID);
91 $this->Query_ID = 0;
92 }
93
94 /* public: perform a query */
95 function query($Query_String) {
96 /* No empty queries, please, since PHP4 chokes on them. */
97 if ($Query_String == "")
98 /* The empty query string is passed on from the constructor,
99 * when calling the class without a query, e.g. in situations
100 * like these: '$db = new DB_Sql_Subclass;'
101 */
102 return 0;
103
104 if (!$this->connect()) {
105 return 0; /* we already complained in connect() about that. */
106 };
107
108 # New query, discard previous result.
109 if ($this->Query_ID) {
110 $this->free();
111 }
112
113 if ($this->Debug)
114 printf("Debug: query = %s<br>\n", $Query_String);
115
116 $this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
117 $this->Row = 0;
118 $this->Errno = mysql_errno();
119 $this->Error = mysql_error();
120 if (!$this->Query_ID) {
121 $this->halt("Invalid SQL: ".$Query_String);
122 }
123
124 # Will return nada if it fails. That's fine.
125 return $this->Query_ID;
126 }
127
128 /* public: walk result set */
129 function next_record() {
130 if (!$this->Query_ID) {
131 $this->halt("next_record called with no query pending.");
132 return 0;
133 }
134
135 $this->Record = @mysql_fetch_array($this->Query_ID);
136 $this->Row += 1;
137 $this->Errno = mysql_errno();
138 $this->Error = mysql_error();
139
140 $stat = is_array($this->Record);
141 if (!$stat && $this->Auto_Free) {
142 $this->free();
143 }
144 return $stat;
145 }
146
147 /* public: position in result set */
148 function seek($pos = 0) {
149 $status = @mysql_data_seek($this->Query_ID, $pos);
150 if ($status)
151 $this->Row = $pos;
152 else {
153 $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows");
154
155 /* half assed attempt to save the day,
156 * but do not consider this documented or even
157 * desireable behaviour.
158 */
159 @mysql_data_seek($this->Query_ID, $this->num_rows());
160 $this->Row = $this->num_rows;
161 return 0;
162 }
163
164 return 1;
165 }
166
167 /* public: table locking */
168 function lock($table, $mode="write") {
169 $this->connect();
170
171 $query="lock tables ";
172 if (is_array($table)) {
173 while (list($key,$value)=each($table)) {
174 if ($key=="read" && $key!=0) {
175 $query.="$value read, ";
176 } else {
177 $query.="$value $mode, ";
178 }
179 }
180 $query=substr($query,0,-2);
181 } else {
182 $query.="$table $mode";
183 }
184 $res = @mysql_query($query, $this->Link_ID);
185 if (!$res) {
186 $this->halt("lock($table, $mode) failed.");
187 return 0;
188 }
189 return $res;
190 }
191
192 function unlock() {
193 $this->connect();
194
195 $res = @mysql_query("unlock tables");
196 if (!$res) {
197 $this->halt("unlock() failed.");
198 return 0;
199 }
200 return $res;
201 }
202
203
204 /* public: evaluate the result (size, width) */
205 function affected_rows() {
206 return @mysql_affected_rows($this->Link_ID);
207 }
208
209 function num_rows() {
210 return @mysql_num_rows($this->Query_ID);
211 }
212
213 function num_fields() {
214 return @mysql_num_fields($this->Query_ID);
215 }
216
217 /* public: shorthand notation */
218 function nf() {
219 return $this->num_rows();
220 }
221
222 function np() {
223 print $this->num_rows();
224 }
225
226 function f($Name) {
227 return $this->Record[$Name];
228 }
229
230 function p($Name) {
231 print $this->Record[$Name];
232 }
233
234 /* public: sequence numbers */
235 function nextid($seq_name) {
236 $this->connect();
237
238 if ($this->lock($this->Seq_Table)) {
239 /* get sequence number (locked) and increment */
240 $q = sprintf("select nextid from %s where seq_name = '%s'",
241 $this->Seq_Table,
242 $seq_name);
243 $id = @mysql_query($q, $this->Link_ID);
244 $res = @mysql_fetch_array($id);
245
246 /* No current value, make one */
247 if (!is_array($res)) {
248 $currentid = 0;
249 $q = sprintf("insert into %s values('%s', %s)",
250 $this->Seq_Table,
251 $seq_name,
252 $currentid);
253 $id = @mysql_query($q, $this->Link_ID);
254 } else {
255 $currentid = $res["nextid"];
256 }
257 $nextid = $currentid + 1;
258 $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
259 $this->Seq_Table,
260 $nextid,
261 $seq_name);
262 $id = @mysql_query($q, $this->Link_ID);
263 $this->unlock();
264 } else {
265 $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
266 return 0;
267 }
268 return $nextid;
269 }
270
271 /* public: return table metadata */
272 function metadata($table='',$full=false) {
273 $count = 0;
274 $id = 0;
275 $res = array();
276
277 /*
278 * Due to compatibility problems with Table we changed the behavior
279 * of metadata();
280 * depending on $full, metadata returns the following values:
281 *
282 * - full is false (default):
283 * $result[]:
284 * [0]["table"] table name
285 * [0]["name"] field name
286 * [0]["type"] field type
287 * [0]["len"] field length
288 * [0]["flags"] field flags
289 *
290 * - full is true
291 * $result[]:
292 * ["num_fields"] number of metadata records
293 * [0]["table"] table name
294 * [0]["name"] field name
295 * [0]["type"] field type
296 * [0]["len"] field length
297 * [0]["flags"] field flags
298 * ["meta"][field name] index of field named "field name"
299 * The last one is used, if you have a field name, but no index.
300 * Test: if (isset($result['meta']['myfield'])) { ...
301 */
302
303 // if no $table specified, assume that we are working with a query
304 // result
305 if ($table) {
306 $this->connect();
307 $id = @mysql_list_fields($this->Database, $table);
308 if (!$id)
309 $this->halt("Metadata query failed.");
310 } else {
311 $id = $this->Query_ID;
312 if (!$id)
313 $this->halt("No query specified.");
314 }
315
316 $count = @mysql_num_fields($id);
317
318 // made this IF due to performance (one if is faster than $count if's)
319 if (!$full) {
320 for ($i=0; $i<$count; $i++) {
321 $res[$i]["table"] = @mysql_field_table ($id, $i);
322 $res[$i]["name"] = @mysql_field_name ($id, $i);
323 $res[$i]["type"] = @mysql_field_type ($id, $i);
324 $res[$i]["len"] = @mysql_field_len ($id, $i);
325 $res[$i]["flags"] = @mysql_field_flags ($id, $i);
326 }
327 } else { // full
328 $res["num_fields"]= $count;
329
330 for ($i=0; $i<$count; $i++) {
331 $res[$i]["table"] = @mysql_field_table ($id, $i);
332 $res[$i]["name"] = @mysql_field_name ($id, $i);
333 $res[$i]["type"] = @mysql_field_type ($id, $i);
334 $res[$i]["len"] = @mysql_field_len ($id, $i);
335 $res[$i]["flags"] = @mysql_field_flags ($id, $i);
336 $res["meta"][$res[$i]["name"]] = $i;
337 }
338 }
339
340 // free the result only if we were called on a table
341 if ($table) @mysql_free_result($id);
342 return $res;
343 }
344
345 /* private: error handling */
346 function halt($msg) {
347 $this->Error = @mysql_error($this->Link_ID);
348 $this->Errno = @mysql_errno($this->Link_ID);
349 if ($this->Halt_On_Error == "no")
350 return;
351
352 $this->haltmsg($msg);
353
354 if ($this->Halt_On_Error != "report")
355 die("Session halted.");
356 }
357
358 function haltmsg($msg) {
359 printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
360 printf("<b>MySQL Error</b>: %s (%s)<br>\n",
361 $this->Errno,
362 $this->Error);
363 }
364
365 function table_names() {
366 $this->query("SHOW TABLES");
367 $i=0;
368 while ($info=mysql_fetch_row($this->Query_ID))
369 {
370 $return[$i]["table_name"]= $info[0];
371 $return[$i]["tablespace_name"]=$this->Database;
372 $return[$i]["database"]=$this->Database;
373 $i++;
374 }
375 return $return;
376 }
377 }
378 ?>