Subversion Repositories logviewer

Rev

Rev 6 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
4 daniel-mar 3
/*
4
 * ViaThinkSoft LogViewer
9 daniel-mar 5
 * Copyright 2018-2022 Daniel Marschall, ViaThinkSoft
4 daniel-mar 6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
6 daniel-mar 20
// Please replace this code with your actual implementation
2 daniel-mar 21
 
6 daniel-mar 22
db_connect('localhost', 'root', '');
23
db_select_db('logviewer');
24
 
25
# ---
26
 
27
// Liefert die Anzahl der Zeilen im Ergebnis
28
function db_num_rows($result) {
29
        if (!$result) {
30
                $err = db_error();
31
                throw new Exception("Called db_num_rows() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
32
        }
33
        return $result->num_rows;
34
}
35
 
36
// Liefert eine Ergebniszeile als Objekt
37
function db_fetch_object($result, $class_name="stdClass", $params=null) {
38
        if (!$result) {
39
                $err = db_error();
40
                throw new Exception("Called db_fetch_object() with an erroneous argument.".($err == '' ? '' : " Possible cause: $err"));
41
        }
42
        if ($params) {
43
                return $result->fetch_object($class_name, $params);
44
        } else {
45
                return $result->fetch_object($class_name);
46
        }
47
}
48
 
49
// Öffnet eine Verbindung zu einem MySQL-Server
50
function db_connect($server=null, $username=null, $password=null, $new_link=false, $client_flags=0) {
51
        global $vts_mysqli;
52
        $ary = explode(':', $server);
53
        $host = $ary[0];
54
        $ini_port = ini_get("mysqli.default_port");
55
        $port = isset($ary[1]) ? (int)$ary[1] : ($ini_port ? (int)$ini_port : 3306);
56
        if (is_null($server)) $port = ini_get("mysqli.default_host");
57
        if (is_null($username)) $port = ini_get("mysqli.default_user");
58
        if (is_null($password)) $port = ini_get("mysqli.default_password");
59
        $vts_mysqli = new mysqli($host, $username, $password, /*dbname*/'', $port, ini_get("mysqli.default_socket"));
60
        return (empty($vts_mysqli->connect_error) && ($vts_mysqli->connect_errno == 0)) ? $vts_mysqli : false;
61
}
62
 
63
// Schließt eine Verbindung zu MySQL
64
function db_close($link_identifier=NULL) {
65
        global $vts_mysqli;
66
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
67
        if (is_null($li)) throw new Exception("Cannot execute db_close(). No valid connection to server.");
68
 
69
        return $li->close();
70
}
71
 
72
// Liefert den Fehlertext der zuvor ausgeführten MySQL Operation
73
function db_error($link_identifier=NULL) {
74
        global $vts_mysqli;
75
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
76
        if (is_null($li)) throw new Exception("Cannot execute db_error(). No valid connection to server.");
77
 
78
        return !empty($li->connect_error) ? $li->connect_error : $li->error;
79
}
80
 
81
// Maskiert spezielle Zeichen innerhalb eines Strings für die Verwendung in einer SQL-Anweisung
82
function db_real_escape_string($unescaped_string, $link_identifier=NULL) {
83
        global $vts_mysqli;
84
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
85
        if (is_null($li)) throw new Exception("Cannot execute db_real_escape_string(). No valid connection to server.");
86
 
87
        return $li->escape_string($unescaped_string);
88
}
89
 
90
// Sendet eine Anfrage an MySQL
91
function db_query($query, $link_identifier=NULL) {
92
        global $vts_mysqli;
93
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
94
        if (is_null($li)) throw new Exception("Cannot execute db_query(). No valid connection to server.");
95
 
96
        return $li->query($query, $resultmode=MYSQLI_STORE_RESULT);
97
}
98
 
99
// Auswahl einer MySQL Datenbank
100
function db_select_db($database_name, $link_identifier=NULL) {
101
        global $vts_mysqli;
102
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
103
        if (is_null($li)) throw new Exception("Cannot execute db_select_db(). No valid connection to server.");
104
 
105
        return $li->select_db($database_name);
106
}
107
 
108
// Liefert die ID, die in der vorherigen Abfrage erzeugt wurde
109
function db_insert_id($link_identifier=NULL) {
110
        global $vts_mysqli;
111
        $li = is_null($link_identifier) ? $vts_mysqli : $link_identifier;
112
        if (is_null($li)) throw new Exception("Cannot execute db_insert_id(). No valid connection to server.");
113
 
114
        return $li->insert_id;
115
}
116