Subversion Repositories prepend

Rev

Rev 4 | Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
// TODO: test everything
4
// TODO: return values?
5
// TODO: check if we matched all stuff mentioned here: https://www.phpclasses.org/blog/package/9199/post/3-Smoothly-Migrate-your-PHP-Code-using-the-Old-MySQL-extension-to-MySQLi.html
6
 
7
$vts_mysqli = null;
8
 
9
// Liefert die Anzahl betroffener Datensätze einer vorhergehenden MySQL Operation
10
function mysql_affected_rows($link_identifier=NULL) {
11
        global $vts_mysqli;
12
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
13
        if (is_null($li)) throw new Exception("Cannot execute mysql_affected_rows(). No valid connection to server.");
14
 
15
        return $li->affected_rows;
16
}
17
 
18
// Liefert den Namen des Zeichensatzes
19
function mysql_client_encoding($link_identifier=NULL) {
20
        global $vts_mysqli;
21
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
22
        if (is_null($li)) throw new Exception("Cannot execute mysql_client_encoding(). No valid connection to server.");
23
 
24
        return $li->character_set_name();
25
}
26
 
27
// Schließt eine Verbindung zu MySQL
28
function mysql_close($link_identifier=NULL) {
29
        global $vts_mysqli;
30
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
31
        if (is_null($li)) throw new Exception("Cannot execute mysql_close(). No valid connection to server.");
32
 
33
        return $li->close();
34
}
35
 
36
// Öffnet eine Verbindung zu einem MySQL-Server
37
function mysql_connect($server=null, $username=null, $password=null, $new_link=false, $client_flags=0) {
38
        global $vts_mysqli;
39
        $ary = explode(':', $server);
40
        $host = $ary[0];
4 daniel-mar 41
        $ini_port = ini_get("mysqli.default_port");
42
        $port = isset($ary[1]) ? (int)$ary[1] : ($ini_port ? (int)$ini_port : 3306);
2 daniel-mar 43
        if (is_null($server)) $port = ini_get("mysqli.default_host");
44
        if (is_null($username)) $port = ini_get("mysqli.default_user");
45
        if (is_null($password)) $port = ini_get("mysqli.default_password");
46
        $vts_mysqli = new mysqli($host, $username, $password, /*dbname*/'', $port, ini_get("mysqli.default_socket"));
47
        return (empty($vts_mysqli->connect_error) && ($vts_mysqli->connect_errno == 0)) ? $vts_mysqli : false;
48
}
49
 
50
// Anlegen einer MySQL-Datenbank
51
function mysql_create_db($database_name, $link_identifier=NULL) {
52
        global $vts_mysqli;
53
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
54
        if (is_null($li)) throw new Exception("Cannot execute mysql_create_db(). No valid connection to server.");
55
 
56
        return mysql_query("CREATE DATABASE `$database_name`", $li) !== false;
57
}
58
 
59
// Bewegt den internen Ergebnis-Zeiger
60
function mysql_data_seek($result, $row_number) {
61
        if (!$result) {
62
                $err = mysql_error();
63
                throw new Exception("Called mysql_data_seek() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
64
        }
4 daniel-mar 65
        return $result->data_seek($row_number) !== false;
2 daniel-mar 66
}
67
 
68
// Liefert Schema Namen vom Aufruf von mysql_list_dbs
69
function mysql_db_name($result, $row, $field=NULL) {
70
        if (!$result) {
71
                $err = mysql_error();
72
                throw new Exception("Called mysql_db_name() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
73
        }
74
        $result->data_seek($row);
75
        return mysql_fetch_array($result)[is_null($field) ? 0 : $field];
76
}
77
 
78
// Selektiert ein Schema und führt in ihm Anfrage aus
79
function mysql_db_query($database, $query, $link_identifier=NULL) {
80
        global $vts_mysqli;
81
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
82
        if (is_null($li)) throw new Exception("Cannot execute mysql_db_query(). No valid connection to server.");
83
 
84
        mysql_select_db($database, $li);
85
        return mysql_query($query, $li);
86
        // Note: The mysql_*() implementation defines, that we will not jump back to our original DB
87
}
88
 
89
// Löschen eines Schemas
90
function mysql_drop_db($database_name, $link_identifier=NULL) {
91
        global $vts_mysqli;
92
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
93
        if (is_null($li)) throw new Exception("Cannot execute mysql_drop_db(). No valid connection to server.");
94
 
95
        return mysql_query("DROP DATABASE `$database_name`", $li) !== false;
96
}
97
 
98
// Liefert die Nummer einer Fehlermeldung einer zuvor ausgeführten MySQL Operation
99
function mysql_errno($link_identifier=NULL) {
100
        global $vts_mysqli;
101
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
102
        if (is_null($li)) throw new Exception("Cannot execute mysql_errno(). No valid connection to server.");
103
 
104
        return !empty($li->connect_errno) ? $li->connect_errno : $li->errno;
105
}
106
 
107
// Liefert den Fehlertext der zuvor ausgeführten MySQL Operation
108
function mysql_error($link_identifier=NULL) {
109
        global $vts_mysqli;
110
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
111
        if (is_null($li)) throw new Exception("Cannot execute mysql_error(). No valid connection to server.");
112
 
113
        return !empty($li->connect_error) ? $li->connect_error : $li->error;
114
}
115
 
116
// Maskiert einen String zur Benutzung in einer MySQL Abfrage
117
function mysql_escape_string($unescaped_string) {
118
        global $vts_mysqli;
119
        return $vts_mysqli->real_escape_string($unescaped_string);
120
}
121
 
122
// Liefert einen Datensatz als assoziatives Array, als numerisches Array oder beides
123
define('MYSQL_ASSOC', MYSQLI_ASSOC);
124
define('MYSQL_NUM',   MYSQLI_NUM);
125
define('MYSQL_BOTH',  MYSQLI_BOTH);
126
function mysql_fetch_array($result, $result_type=MYSQL_BOTH) {
127
        if (!$result) {
128
                $err = mysql_error();
129
                throw new Exception("Called mysql_fetch_array() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
130
        }
131
        return $result->fetch_array($result_type);
132
}
133
 
134
// Liefert einen Datensatz als assoziatives Array
135
function mysql_fetch_assoc($result) {
136
        if (!$result) {
137
                $err = mysql_error();
138
                throw new Exception("Called mysql_fetch_assoc() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
139
        }
140
        return $result->fetch_assoc();
141
}
142
 
143
// Liefert ein Objekt mit Feldinformationen aus einem Anfrageergebnis
144
function mysql_fetch_field($result, $field_offset=0) {
145
        if (!$result) {
146
                $err = mysql_error();
147
                throw new Exception("Called mysql_fetch_field() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
148
        }
149
        return $result->fetch_field();
150
}
151
 
152
// Liefert die Länge eines jeden Feldes in einem Ergebnis
153
function mysql_fetch_lengths($result) {
154
        if (!$result) {
155
                $err = mysql_error();
156
                throw new Exception("Called mysql_fetch_lengths() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
157
        }
158
        return $result->lengths;
159
}
160
 
161
// Liefert eine Ergebniszeile als Objekt
162
function mysql_fetch_object($result, $class_name="stdClass", $params=null) {
163
        if (!$result) {
164
                $err = mysql_error();
165
                throw new Exception("Called mysql_fetch_object() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
166
        }
167
        if ($params) {
168
                return $result->fetch_object($class_name, $params);
169
        } else {
170
                return $result->fetch_object($class_name);
171
        }
172
}
173
 
174
// Liefert einen Datensatz als indiziertes Array
175
function mysql_fetch_row($result) {
176
        if (!$result) {
177
                $err = mysql_error();
178
                throw new Exception("Called mysql_fetch_row() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
179
        }
180
        return $result->fetch_row();
181
}
182
 
183
// Liefert die Flags des spezifizierten Feldes in einem Anfrageergebnis
184
function mysql_field_flags($result, $field_offset) {
185
        if (!$result) {
186
                $err = mysql_error();
187
                throw new Exception("Called mysql_field_flags() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
188
        }
4 daniel-mar 189
        return $result->fetch_field_direct($field_offset)->flags;
2 daniel-mar 190
}
191
 
192
// Liefert die Länge des angegebenen Feldes
193
function mysql_field_len($result, $field_offset) {
194
        if (!$result) {
195
                $err = mysql_error();
196
                throw new Exception("Called mysql_field_len() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
197
        }
4 daniel-mar 198
        return $result->fetch_field_direct($field_offset)->length;
2 daniel-mar 199
}
200
 
201
// Liefert den Namen eines Feldes in einem Ergebnis
202
function mysql_field_name($result, $field_offset) {
203
        if (!$result) {
204
                $err = mysql_error();
205
                throw new Exception("Called mysql_field_name() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
206
        }
5 daniel-mar 207
        return $result->fetch_field_direct($field_offset)->name; //or "orgname"
2 daniel-mar 208
}
209
 
210
// Setzt den Ergebniszeiger auf ein bestimmtes Feldoffset
211
function mysql_field_seek($result, $field_offset) {
212
        if (!$result) {
213
                $err = mysql_error();
214
                throw new Exception("Called mysql_field_seek() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
215
        }
216
        return $result->field_seek($field_offset);
217
}
218
 
219
// Liefert den Namen der Tabelle, die das genannte Feld enthält
220
function mysql_field_table($result, $field_offset) {
221
        if (!$result) {
222
                $err = mysql_error();
223
                throw new Exception("Called mysql_field_table() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
224
        }
225
        return $result->fetch_field_direct($field_offset)->table; // or "orgtable"
226
}
227
 
228
// Liefert den Typ des spezifizierten Feldes in einem Ergebnis
229
function mysql_field_type($result, $field_offset) {
230
        if (!$result) {
231
                $err = mysql_error();
232
                throw new Exception("Called mysql_field_type() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
233
        }
234
        return $result->fetch_field_direct($field_offset)->type;
235
}
236
 
237
// Gibt belegten Speicher wieder frei
238
function mysql_free_result($result) {
239
        if (!$result) {
240
                $err = mysql_error();
241
                throw new Exception("Called mysql_free_result() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
242
        }
243
        return $result->free();
244
}
245
 
246
// Liefert MySQL Clientinformationen
247
function mysql_get_client_info() {
248
        global $vts_mysqli;
249
        return $vts_mysqli->get_client_info();
250
}
251
 
252
// Liefert MySQL Host Informationen
253
function mysql_get_host_info($link_identifier=NULL) {
254
        global $vts_mysqli;
255
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
256
        if (is_null($li)) throw new Exception("Cannot execute mysql_get_host_info(). No valid connection to server.");
257
 
258
        return $li->host_info;
259
}
260
 
261
// Liefert MySQL Protokollinformationen
262
function mysql_get_proto_info($link_identifier=NULL) {
263
        global $vts_mysqli;
264
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
265
        if (is_null($li)) throw new Exception("Cannot execute mysql_get_proto_info(). No valid connection to server.");
266
 
267
        return $li->protocol_version;
268
}
269
 
270
// Liefert MySQL Server Informationen
271
function mysql_get_server_info($link_identifier=NULL) {
272
        global $vts_mysqli;
273
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
274
        if (is_null($li)) throw new Exception("Cannot execute mysql_get_server_info(). No valid connection to server.");
275
 
276
        return $li->server_info;
277
}
278
 
279
// Liefert Informationen über die zuletzt ausgeführte Anfrage zurück
280
function mysql_info($link_identifier=NULL) {
281
        global $vts_mysqli;
282
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
283
        if (is_null($li)) throw new Exception("Cannot execute mysql_info(). No valid connection to server.");
284
 
285
        return $li->info;
286
}
287
 
288
// Liefert die ID, die in der vorherigen Abfrage erzeugt wurde
289
function mysql_insert_id($link_identifier=NULL) {
290
        global $vts_mysqli;
291
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
292
        if (is_null($li)) throw new Exception("Cannot execute mysql_insert_id(). No valid connection to server.");
293
 
294
        return $li->insert_id;
295
}
296
 
297
// Auflistung der verfügbaren Datenbanken (Schemata) auf einem MySQL Server
298
function mysql_list_dbs($link_identifier=NULL) {
299
        global $vts_mysqli;
300
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
301
        if (is_null($li)) throw new Exception("Cannot execute mysql_list_dbs(). No valid connection to server.");
302
 
303
        return mysql_query('SHOW DATABASES', $li);
304
}
305
 
306
// Listet MySQL Tabellenfelder auf
307
function mysql_list_fields($database_name, $table_name, $link_identifier=NULL) {
308
        global $vts_mysqli;
309
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
310
        if (is_null($li)) throw new Exception("Cannot execute mysql_list_fields(). No valid connection to server.");
311
 
312
        return mysql_query("SHOW COLUMNS FROM `$database_name`.`$table_name`", $li);
313
}
314
 
315
// Zeigt die MySQL Prozesse an
316
function mysql_list_processes($link_identifier=NULL) {
317
        global $vts_mysqli;
318
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
319
        if (is_null($li)) throw new Exception("Cannot execute mysql_list_processes(). No valid connection to server.");
320
 
321
        return $li->thread_id;
322
}
323
 
324
// Listet Tabellen in einer MySQL Datenbank auf
325
function mysql_list_tables($database, $link_identifier=NULL) {
326
        global $vts_mysqli;
327
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
328
        if (is_null($li)) throw new Exception("Cannot execute mysql_list_tables(). No valid connection to server.");
329
 
330
        return mysql_query("SHOW TABLES FROM `$database`", $li);
331
}
332
 
333
// Liefert die Anzahl der Felder in einem Ergebnis
334
function mysql_num_fields($result) {
335
        if (!$result) {
336
                $err = mysql_error();
337
                throw new Exception("Called mysql_num_fields() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
338
        }
339
        global $vts_mysqli;
340
        return $vts_mysqli->field_count;
341
}
342
 
343
// Liefert die Anzahl der Zeilen im Ergebnis
344
function mysql_num_rows($result) {
345
        if (!$result) {
346
                $err = mysql_error();
347
                throw new Exception("Called mysql_num_rows() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
348
        }
349
        return $result->num_rows;
350
}
351
 
352
// Öffnet eine persistente Verbindung zum MySQL Server
353
function mysql_pconnect($server=null, $username=null, $password=null, $client_flags=0) {
354
        global $vts_mysqli;
355
        $ary = explode(':', $server);
356
        $host = $ary[0];
4 daniel-mar 357
        $ini_port = ini_get("mysqli.default_port");
358
        $port = isset($ary[1]) ? (int)$ary[1] : ($ini_port ? (int)$ini_port : 3306);
2 daniel-mar 359
        if (is_null($server)) $port = ini_get("mysqli.default_host");
360
        if (is_null($username)) $port = ini_get("mysqli.default_user");
361
        if (is_null($password)) $port = ini_get("mysqli.default_password");
362
        $vts_mysqli = new mysqli('p:'.$host, $username, $password, /*dbname*/'', $port, ini_get("mysqli.default_socket"));
363
        return (empty($vts_mysqli->connect_error) && ($vts_mysqli->connect_errno == 0)) ? $vts_mysqli : false;
364
}
365
 
366
// Ping a server connection or reconnect if there is no connection
367
function mysql_ping($link_identifier=NULL) {
368
        global $vts_mysqli;
369
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
370
        if (is_null($li)) throw new Exception("Cannot execute mysql_ping(). No valid connection to server.");
371
 
372
        return $li->ping();
373
}
374
 
375
// Sendet eine Anfrage an MySQL
376
function mysql_query($query, $link_identifier=NULL) {
377
        global $vts_mysqli;
378
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
379
        if (is_null($li)) throw new Exception("Cannot execute mysql_query(). No valid connection to server.");
380
 
381
        return $li->query($query, $resultmode=MYSQLI_STORE_RESULT);
382
}
383
 
384
// Maskiert spezielle Zeichen innerhalb eines Strings für die Verwendung in einer SQL-Anweisung
385
function mysql_real_escape_string($unescaped_string, $link_identifier=NULL) {
386
        global $vts_mysqli;
387
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
388
        if (is_null($li)) throw new Exception("Cannot execute mysql_real_escape_string(). No valid connection to server.");
389
 
390
        return $li->escape_string($unescaped_string);
391
}
392
 
393
// Liefert Ergebnis
394
function mysql_result($result, $row, $field=0) {
395
        if (!$result) {
396
                $err = mysql_error();
397
                throw new Exception("Called mysql_result() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
398
        }
399
        $result->data_seek($row);
400
        return mysql_fetch_array($result)[$field];
401
}
402
 
403
// Auswahl einer MySQL Datenbank
404
function mysql_select_db($database_name, $link_identifier=NULL) {
405
        global $vts_mysqli;
406
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
407
        if (is_null($li)) throw new Exception("Cannot execute mysql_select_db(). No valid connection to server.");
408
 
409
        return $li->select_db($database_name);
410
}
411
 
412
// Setzt den Verbindungszeichensatz
413
function mysql_set_charset($charset, $link_identifier=NULL) {
414
        global $vts_mysqli;
415
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
416
        if (is_null($li)) throw new Exception("Cannot execute mysql_set_charset(). No valid connection to server.");
417
 
418
        return $li->set_charset($charset);
419
}
420
 
421
// Zeigt den momentanen Serverstatus an
422
function mysql_stat($link_identifier=NULL) {
423
        global $vts_mysqli;
424
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
425
        if (is_null($li)) throw new Exception("Cannot execute mysql_stat(). No valid connection to server.");
426
 
427
        return $li->stat();
428
}
429
 
430
// Liefert den Namen einer Tabelle
431
function mysql_tablename($result, $i) {
432
        if (!$result) {
433
                $err = mysql_error();
434
                throw new Exception("Called mysql_tablename() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
435
        }
436
        $result->data_seek($i);
437
        return mysql_fetch_array($result)[0];
438
}
439
 
440
// Zeigt die aktuelle Thread ID an
441
function mysql_thread_id($link_identifier=NULL) {
442
        global $vts_mysqli;
443
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
444
        if (is_null($li)) throw new Exception("Cannot execute mysql_thread_id(). No valid connection to server.");
445
 
446
        return $li->thread_id;
447
}
448
 
449
// Sendet eine SQL Anfrage an MySQL, ohne Ergebniszeilen abzuholen und zu puffern
450
function mysql_unbuffered_query($query, $link_identifier=NULL) {
451
        global $vts_mysqli;
452
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
453
        if (is_null($li)) throw new Exception("Cannot execute mysql_unbuffered_query(). No valid connection to server.");
454
 
455
        // http://php.net/manual/de/mysqlinfo.concepts.buffering.php
456
        // https://stackoverflow.com/questions/1982016/unbuffered-query-with-mysqli?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
457
        $li->real_query($query);
458
        $li->use_result();
459
}
460