Subversion Repositories php_guestbook

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. function verbinden() {
  4.         global $mysql_database, $mysql_server, $mysql_pass, $mysql_user;
  5.  
  6.         if (!db_connect($mysql_server, $mysql_user, $mysql_pass)) {
  7.                 die('<b>Verbindung zum MySQL-Server konnte nicht hergestellt werden! ('.db_error().')</b>');
  8.         }
  9.  
  10.         if (!db_select_db($mysql_database)) {
  11.                 die('<b>Verbindung zum MySQL-Server konnte nicht hergestellt werden! ('.db_error().')</b>');
  12.         }
  13.  
  14.         register_shutdown_function('trennen');
  15.         db_select_db($mysql_database);
  16. }
  17.  
  18. function trennen() {
  19.         @db_close();
  20. }
  21.  
  22. // Liefert die Anzahl der Zeilen im Ergebnis
  23. function db_num_rows($result) {
  24.         if (!$result) {
  25.                 $err = db_error();
  26.                 throw new Exception("Called db_num_rows() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
  27.         }
  28.         return $result->num_rows;
  29. }
  30.  
  31. // Liefert eine Ergebniszeile als Objekt
  32. function db_fetch_object($result, $class_name="stdClass", $params=null) {
  33.         if (!$result) {
  34.                 $err = db_error();
  35.                 throw new Exception("Called db_fetch_object() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
  36.         }
  37.         if ($params) {
  38.                 return $result->fetch_object($class_name, $params);
  39.         } else {
  40.                 return $result->fetch_object($class_name);
  41.         }
  42. }
  43.  
  44. // Öffnet eine Verbindung zu einem MySQL-Server
  45. function db_connect($server=null, $username=null, $password=null, $new_link=false, $client_flags=0) {
  46.         global $vts_mysqli;
  47.         $ary = explode(':', $server);
  48.         $host = $ary[0];
  49.         $ini_port = ini_get("mysqli.default_port");
  50.         $port = isset($ary[1]) ? (int)$ary[1] : ($ini_port ? (int)$ini_port : 3306);
  51.         if (is_null($server)) $port = ini_get("mysqli.default_host");
  52.         if (is_null($username)) $port = ini_get("mysqli.default_user");
  53.         if (is_null($password)) $port = ini_get("mysqli.default_password");
  54.         $vts_mysqli = new mysqli($host, $username, $password, /*dbname*/'', $port, ini_get("mysqli.default_socket"));
  55.         return (empty($vts_mysqli->connect_error) && ($vts_mysqli->connect_errno == 0)) ? $vts_mysqli : false;
  56. }
  57.  
  58. // Schließt eine Verbindung zu MySQL
  59. function db_close($link_identifier=NULL) {
  60.         global $vts_mysqli;
  61.         $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
  62.         if (is_null($li)) throw new Exception("Cannot execute db_close(). No valid connection to server.");
  63.  
  64.         return $li->close();
  65. }
  66.  
  67. // Liefert den Fehlertext der zuvor ausgeführten MySQL Operation
  68. function db_error($link_identifier=NULL) {
  69.         global $vts_mysqli;
  70.         $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
  71.         if (is_null($li)) throw new Exception("Cannot execute db_error(). No valid connection to server.");
  72.  
  73.         return !empty($li->connect_error) ? $li->connect_error : $li->error;
  74. }
  75.  
  76. // Maskiert spezielle Zeichen innerhalb eines Strings für die Verwendung in einer SQL-Anweisung
  77. function db_real_escape_string($unescaped_string, $link_identifier=NULL) {
  78.         global $vts_mysqli;
  79.         $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
  80.         if (is_null($li)) throw new Exception("Cannot execute db_real_escape_string(). No valid connection to server.");
  81.  
  82.         return $li->escape_string($unescaped_string);
  83. }
  84.  
  85. // Sendet eine Anfrage an MySQL
  86. function db_query($query, $link_identifier=NULL) {
  87.         global $vts_mysqli;
  88.         $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
  89.         if (is_null($li)) throw new Exception("Cannot execute db_query(). No valid connection to server.");
  90.  
  91.         return $li->query($query, $resultmode=MYSQLI_STORE_RESULT);
  92. }
  93.  
  94. // Auswahl einer MySQL Datenbank
  95. function db_select_db($database_name, $link_identifier=NULL) {
  96.         global $vts_mysqli;
  97.         $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
  98.         if (is_null($li)) throw new Exception("Cannot execute db_select_db(). No valid connection to server.");
  99.  
  100.         return $li->select_db($database_name);
  101. }
  102.  
  103. // Liefert die ID, die in der vorherigen Abfrage erzeugt wurde
  104. function db_insert_id($link_identifier=NULL) {
  105.         global $vts_mysqli;
  106.         $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
  107.         if (is_null($li)) throw new Exception("Cannot execute db_insert_id(). No valid connection to server.");
  108.  
  109.         return $li->insert_id;
  110. }
  111.