Rev 5 | Rev 9 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5 | Rev 8 | ||
---|---|---|---|
Line 3... | Line 3... | ||
3 | // TODO: test everything |
3 | // TODO: test everything |
4 | // TODO: return values? |
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 |
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 | 6 | ||
7 | $vts_mysqli = null; |
7 | $vts_mysqli = null; |
- | 8 | $vts_mysqli_report_set_once = false; |
|
8 | 9 | ||
9 | // Liefert die Anzahl betroffener Datensätze einer vorhergehenden MySQL Operation |
10 | // Liefert die Anzahl betroffener Datensätze einer vorhergehenden MySQL Operation |
10 | function mysql_affected_rows($link_identifier=NULL) { |
11 | function mysql_affected_rows($link_identifier=NULL) { |
11 | global $vts_mysqli; |
12 | global $vts_mysqli; |
12 | $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier; |
13 | $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 | if (is_null($li)) throw new Exception("Cannot execute mysql_affected_rows(). No valid connection to server."); |
14 | 15 | ||
Line 22... | Line 23... | ||
22 | if (is_null($li)) throw new Exception("Cannot execute mysql_client_encoding(). No valid connection to server."); |
23 | if (is_null($li)) throw new Exception("Cannot execute mysql_client_encoding(). No valid connection to server."); |
23 | 24 | ||
24 | return $li->character_set_name(); |
25 | return $li->character_set_name(); |
25 | } |
26 | } |
26 | 27 | ||
27 | // Schließt eine Verbindung zu MySQL |
28 | // Schließt eine Verbindung zu MySQL |
28 | function mysql_close($link_identifier=NULL) { |
29 | function mysql_close($link_identifier=NULL) { |
29 | global $vts_mysqli; |
30 | global $vts_mysqli; |
30 | $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier; |
31 | $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 | if (is_null($li)) throw new Exception("Cannot execute mysql_close(). No valid connection to server."); |
32 | 33 | ||
33 | return $li->close(); |
34 | return $li->close(); |
34 | } |
35 | } |
35 | 36 | ||
36 | // Öffnet eine Verbindung zu einem MySQL-Server |
37 | // Öffnet eine Verbindung zu einem MySQL-Server |
37 | function mysql_connect($server=null, $username=null, $password=null, $new_link=false, $client_flags=0) { |
38 | function mysql_connect($server=null, $username=null, $password=null, $new_link=false, $client_flags=0) { |
38 | global $vts_mysqli; |
39 | global $vts_mysqli; |
- | 40 | global $vts_mysqli_report_set_once; |
|
39 | $ary = explode(':', $server); |
41 | $ary = explode(':', $server); |
40 | $host = $ary[0]; |
42 | $host = $ary[0]; |
41 | $ini_port = ini_get("mysqli.default_port"); |
43 | $ini_port = ini_get("mysqli.default_port"); |
42 | $port = isset($ary[1]) ? (int)$ary[1] : ($ini_port ? (int)$ini_port : 3306); |
44 | $port = isset($ary[1]) ? (int)$ary[1] : ($ini_port ? (int)$ini_port : 3306); |
43 | if (is_null($server)) $port = ini_get("mysqli.default_host"); |
45 | if (is_null($server)) $port = ini_get("mysqli.default_host"); |
44 | if (is_null($username)) $port = ini_get("mysqli.default_user"); |
46 | if (is_null($username)) $port = ini_get("mysqli.default_user"); |
45 | if (is_null($password)) $port = ini_get("mysqli.default_password"); |
47 | 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")); |
48 | $vts_mysqli = new mysqli($host, $username, $password, /*dbname*/'', $port, ini_get("mysqli.default_socket")); |
- | 49 | if (!$vts_mysqli_report_set_once) { |
|
- | 50 | mysqli_report(MYSQLI_REPORT_OFF); // PHP <8.1 compatibility |
|
- | 51 | $vts_mysqli_report_set_once = true; |
|
- | 52 | } |
|
47 | return (empty($vts_mysqli->connect_error) && ($vts_mysqli->connect_errno == 0)) ? $vts_mysqli : false; |
53 | return (empty($vts_mysqli->connect_error) && ($vts_mysqli->connect_errno == 0)) ? $vts_mysqli : false; |
48 | } |
54 | } |
49 | 55 | ||
50 | // Anlegen einer MySQL-Datenbank |
56 | // Anlegen einer MySQL-Datenbank |
51 | function mysql_create_db($database_name, $link_identifier=NULL) { |
57 | function mysql_create_db($database_name, $link_identifier=NULL) { |
Line 73... | Line 79... | ||
73 | } |
79 | } |
74 | $result->data_seek($row); |
80 | $result->data_seek($row); |
75 | return mysql_fetch_array($result)[is_null($field) ? 0 : $field]; |
81 | return mysql_fetch_array($result)[is_null($field) ? 0 : $field]; |
76 | } |
82 | } |
77 | 83 | ||
78 | // Selektiert ein Schema und führt in ihm Anfrage aus |
84 | // Selektiert ein Schema und führt in ihm Anfrage aus |
79 | function mysql_db_query($database, $query, $link_identifier=NULL) { |
85 | function mysql_db_query($database, $query, $link_identifier=NULL) { |
80 | global $vts_mysqli; |
86 | global $vts_mysqli; |
81 | $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier; |
87 | $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."); |
88 | if (is_null($li)) throw new Exception("Cannot execute mysql_db_query(). No valid connection to server."); |
83 | 89 | ||
84 | mysql_select_db($database, $li); |
90 | mysql_select_db($database, $li); |
85 | return mysql_query($query, $li); |
91 | return mysql_query($query, $li); |
86 | // Note: The mysql_*() implementation defines, that we will not jump back to our original DB |
92 | // Note: The mysql_*() implementation defines, that we will not jump back to our original DB |
87 | } |
93 | } |
88 | 94 | ||
89 | // Löschen eines Schemas |
95 | // Löschen eines Schemas |
90 | function mysql_drop_db($database_name, $link_identifier=NULL) { |
96 | function mysql_drop_db($database_name, $link_identifier=NULL) { |
91 | global $vts_mysqli; |
97 | global $vts_mysqli; |
92 | $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier; |
98 | $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."); |
99 | if (is_null($li)) throw new Exception("Cannot execute mysql_drop_db(). No valid connection to server."); |
94 | 100 | ||
95 | return mysql_query("DROP DATABASE `$database_name`", $li) !== false; |
101 | return mysql_query("DROP DATABASE `$database_name`", $li) !== false; |
96 | } |
102 | } |
97 | 103 | ||
98 | // Liefert die Nummer einer Fehlermeldung einer zuvor ausgeführten MySQL Operation |
104 | // Liefert die Nummer einer Fehlermeldung einer zuvor ausgeführten MySQL Operation |
99 | function mysql_errno($link_identifier=NULL) { |
105 | function mysql_errno($link_identifier=NULL) { |
100 | global $vts_mysqli; |
106 | global $vts_mysqli; |
101 | $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier; |
107 | $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."); |
108 | if (is_null($li)) throw new Exception("Cannot execute mysql_errno(). No valid connection to server."); |
103 | 109 | ||
104 | return !empty($li->connect_errno) ? $li->connect_errno : $li->errno; |
110 | return !empty($li->connect_errno) ? $li->connect_errno : $li->errno; |
105 | } |
111 | } |
106 | 112 | ||
107 | // Liefert den Fehlertext der zuvor ausgeführten MySQL Operation |
113 | // Liefert den Fehlertext der zuvor ausgeführten MySQL Operation |
108 | function mysql_error($link_identifier=NULL) { |
114 | function mysql_error($link_identifier=NULL) { |
109 | global $vts_mysqli; |
115 | global $vts_mysqli; |
110 | $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier; |
116 | $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."); |
117 | if (is_null($li)) throw new Exception("Cannot execute mysql_error(). No valid connection to server."); |
112 | 118 | ||
Line 147... | Line 153... | ||
147 | throw new Exception("Called mysql_fetch_field() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
153 | throw new Exception("Called mysql_fetch_field() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
148 | } |
154 | } |
149 | return $result->fetch_field(); |
155 | return $result->fetch_field(); |
150 | } |
156 | } |
151 | 157 | ||
152 | // Liefert die Länge eines jeden Feldes in einem Ergebnis |
158 | // Liefert die Länge eines jeden Feldes in einem Ergebnis |
153 | function mysql_fetch_lengths($result) { |
159 | function mysql_fetch_lengths($result) { |
154 | if (!$result) { |
160 | if (!$result) { |
155 | $err = mysql_error(); |
161 | $err = mysql_error(); |
156 | throw new Exception("Called mysql_fetch_lengths() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
162 | throw new Exception("Called mysql_fetch_lengths() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
157 | } |
163 | } |
Line 187... | Line 193... | ||
187 | throw new Exception("Called mysql_field_flags() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
193 | throw new Exception("Called mysql_field_flags() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
188 | } |
194 | } |
189 | return $result->fetch_field_direct($field_offset)->flags; |
195 | return $result->fetch_field_direct($field_offset)->flags; |
190 | } |
196 | } |
191 | 197 | ||
192 | // Liefert die Länge des angegebenen Feldes |
198 | // Liefert die Länge des angegebenen Feldes |
193 | function mysql_field_len($result, $field_offset) { |
199 | function mysql_field_len($result, $field_offset) { |
194 | if (!$result) { |
200 | if (!$result) { |
195 | $err = mysql_error(); |
201 | $err = mysql_error(); |
196 | throw new Exception("Called mysql_field_len() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
202 | throw new Exception("Called mysql_field_len() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
197 | } |
203 | } |
Line 214... | Line 220... | ||
214 | throw new Exception("Called mysql_field_seek() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
220 | throw new Exception("Called mysql_field_seek() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
215 | } |
221 | } |
216 | return $result->field_seek($field_offset); |
222 | return $result->field_seek($field_offset); |
217 | } |
223 | } |
218 | 224 | ||
219 | // Liefert den Namen der Tabelle, die das genannte Feld enthält |
225 | // Liefert den Namen der Tabelle, die das genannte Feld enthält |
220 | function mysql_field_table($result, $field_offset) { |
226 | function mysql_field_table($result, $field_offset) { |
221 | if (!$result) { |
227 | if (!$result) { |
222 | $err = mysql_error(); |
228 | $err = mysql_error(); |
223 | throw new Exception("Called mysql_field_table() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
229 | throw new Exception("Called mysql_field_table() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
224 | } |
230 | } |
Line 274... | Line 280... | ||
274 | if (is_null($li)) throw new Exception("Cannot execute mysql_get_server_info(). No valid connection to server."); |
280 | if (is_null($li)) throw new Exception("Cannot execute mysql_get_server_info(). No valid connection to server."); |
275 | 281 | ||
276 | return $li->server_info; |
282 | return $li->server_info; |
277 | } |
283 | } |
278 | 284 | ||
279 | // Liefert Informationen über die zuletzt ausgeführte Anfrage zurück |
285 | // Liefert Informationen über die zuletzt ausgeführte Anfrage zurück |
280 | function mysql_info($link_identifier=NULL) { |
286 | function mysql_info($link_identifier=NULL) { |
281 | global $vts_mysqli; |
287 | global $vts_mysqli; |
282 | $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier; |
288 | $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."); |
289 | if (is_null($li)) throw new Exception("Cannot execute mysql_info(). No valid connection to server."); |
284 | 290 | ||
Line 292... | Line 298... | ||
292 | if (is_null($li)) throw new Exception("Cannot execute mysql_insert_id(). No valid connection to server."); |
298 | if (is_null($li)) throw new Exception("Cannot execute mysql_insert_id(). No valid connection to server."); |
293 | 299 | ||
294 | return $li->insert_id; |
300 | return $li->insert_id; |
295 | } |
301 | } |
296 | 302 | ||
297 | // Auflistung der verfügbaren Datenbanken (Schemata) auf einem MySQL Server |
303 | // Auflistung der verfügbaren Datenbanken (Schemata) auf einem MySQL Server |
298 | function mysql_list_dbs($link_identifier=NULL) { |
304 | function mysql_list_dbs($link_identifier=NULL) { |
299 | global $vts_mysqli; |
305 | global $vts_mysqli; |
300 | $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier; |
306 | $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."); |
307 | if (is_null($li)) throw new Exception("Cannot execute mysql_list_dbs(). No valid connection to server."); |
302 | 308 | ||
Line 347... | Line 353... | ||
347 | throw new Exception("Called mysql_num_rows() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
353 | throw new Exception("Called mysql_num_rows() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err")); |
348 | } |
354 | } |
349 | return $result->num_rows; |
355 | return $result->num_rows; |
350 | } |
356 | } |
351 | 357 | ||
352 | // Öffnet eine persistente Verbindung zum MySQL Server |
358 | // Öffnet eine persistente Verbindung zum MySQL Server |
353 | function mysql_pconnect($server=null, $username=null, $password=null, $client_flags=0) { |
359 | function mysql_pconnect($server=null, $username=null, $password=null, $client_flags=0) { |
354 | global $vts_mysqli; |
360 | global $vts_mysqli; |
355 | $ary = explode(':', $server); |
361 | $ary = explode(':', $server); |
356 | $host = $ary[0]; |
362 | $host = $ary[0]; |
357 | $ini_port = ini_get("mysqli.default_port"); |
363 | $ini_port = ini_get("mysqli.default_port"); |
Line 379... | Line 385... | ||
379 | if (is_null($li)) throw new Exception("Cannot execute mysql_query(). No valid connection to server."); |
385 | if (is_null($li)) throw new Exception("Cannot execute mysql_query(). No valid connection to server."); |
380 | 386 | ||
381 | return $li->query($query, $resultmode=MYSQLI_STORE_RESULT); |
387 | return $li->query($query, $resultmode=MYSQLI_STORE_RESULT); |
382 | } |
388 | } |
383 | 389 | ||
384 | // Maskiert spezielle Zeichen innerhalb eines Strings für die Verwendung in einer SQL-Anweisung |
390 | // 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) { |
391 | function mysql_real_escape_string($unescaped_string, $link_identifier=NULL) { |
386 | global $vts_mysqli; |
392 | global $vts_mysqli; |
387 | $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier; |
393 | $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."); |
394 | if (is_null($li)) throw new Exception("Cannot execute mysql_real_escape_string(). No valid connection to server."); |
389 | 395 |