-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdblib.php
140 lines (117 loc) · 3.11 KB
/
dblib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/*
(c) 2022 Chris Royle
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
function debug($str)
{
global $debug_flag;
global $userdata;
global $node_id;
$s = "";
if (isset($userdata->node_id))
$s = sprintf("NODE %04s", $userdata->node_id);
else if (isset($node_id))
$s = sprintf("NODE %04s", $node_id);
if (isset($userdata) && isset($userdata->user_id))
$s .= sprintf("(%8d):", $userdata->user_id);
else
$s .= "(--------)";
$s .= ":".$str;
if ($debug_flag)
printf("%8d: %s\n", getmypid(), $s);
}
function dbq($s, ...$args)
{
global $dbh, $db_debug_flag;
$msg = null;
$success = false;
if (count($args) == 0)
{
$result = @mysqli_query($dbh, $s);
if (!$result)
{
$msg = @mysqli_error($dbh);
debug("$s -- Query error: $msg");
}
else
{
if ($db_debug_flag) debug("$s -- Successful query - ".@mysqli_num_rows($result)." rows returned");
$success = true;
}
}
else
{
if ($db_debug_flag)
{ $ac = count($args);
debug("Preparing query $s with params ... $ac - ".implode(' - ', $args));
}
$stmt = @mysqli_prepare($dbh, $s);
if (!$stmt)
{
$result = false;
$success = false;
debug ("Prepare failure - ".@mysqli_error($dbh));
}
else
{
if (!@mysqli_stmt_bind_param($stmt, ...$args))
{
$result = false; $success = false;
debug("SQL Param Bind failure - ".@mysqli_stmt_error($stmt));
}
else if (@mysqli_stmt_execute($stmt))
{
$result = @mysqli_stmt_get_result($stmt);
// NB mysqli_stmt_get_result returns false on a query which returns no results
$success = true;
}
else
debug("Stmt Execute problem - ".mysqli_stmt_error($stmt));
}
if (!$success)
{
$msg = @mysqli_stmt_error($stmt);
$params = array();
array_push ($params, ...$args);
debug("$s -- Query error: $msg");
}
else
if ($db_debug_flag) debug("$s -- Successful query - ".@mysqli_num_rows($result)." rows returned");
}
$ret['success'] = $success;
if (!isset($result)) $result = false;
$ret['result'] = $result;
$ret['msg'] = $msg;
$ret['numrows'] = @mysqli_num_rows($result);
$ret['insert_id'] = @mysqli_insert_id($dbh);
if (count($args) == 0)
$ret['affected'] = @mysqli_affected_rows($dbh);
else
$ret['affected'] = @mysqli_stmt_affected_rows($stmt);
return($ret);
}
function dbq_starttransaction()
{
global $dbh;
return(@mysqli_begin_transaction($dbh, MYSQLI_TRANS_START_READ_WRITE));
}
function dbq_rollback()
{
global $dbh;
return(@mysqli_rollback($dbh));
}
function dbq_commit()
{
global $dbh;
return(@mysqli_commit($dbh));
}