Subversion Repositories prepend

Rev

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