Subversion Repositories prepend

Rev

Rev 5 | Rev 9 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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