-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
216 lines (163 loc) · 5.29 KB
/
db.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?
if (!checksetarray($mysqli_info, array ('host', 'username', 'passwd', 'dbname')))
fatal('$mysql_info array not defined or complete '.
'(host, username, passwd, dbane) in '.$configfile);
if (!($GLOBALS['db'] = mysqli_connect($mysqli_info['host'],
$mysqli_info['username'], $mysqli_info['passwd'],
$mysqli_info['dbname']))) {
fatal("mysqli_connect (".mysqli_connect_errno()."): ".
mysqli_connect_error());
}
function fatal_mysqli($function_name) {
fatal($function_name.' ('.mysqli_errno($GLOBALS['db']).'): '.mysqli_error($GLOBALS['db']));
}
if (!mysqli_set_charset($db, "utf8"))
fatal_mysqli('mysqli_set_charset');
// create & execute statement
function db_vce_stmt($query, $args) {
$refs = array();
if (!($refs[0] = mysqli_prepare($GLOBALS['db'], $query))) fatal_mysqli('msqli_prepare');
$refs[1] = '';
if (mysqli_stmt_param_count($refs[0]) != count($args))
fatal('prepared statement expects '.myqsqli_stmt_param_count($refs[0]).' parameter(s) but gets '.count($args).' parameter(s): '.$query);
// bind parameters if there are any
if (count($args)) {
foreach ($args as &$arg) {
$refs[] = &$arg;
$type = gettype($arg);
switch ($type) {
case 'integer':
$refs[1] .= 'i';
break;
case 'string':
$refs[1] .= 's';
break;
case 'NULL':
$refs[1] .= 'i';
break;
default:
fatal('unsupported type ('.$type.') for MySQL query (only integer, NULL and string supported)');
}
}
if (!call_user_func_array('mysqli_stmt_bind_param', $refs)) fatal_mysqli('mysqli_bind_param');
}
if (!mysqli_stmt_execute($refs[0])) fatal_mysqli('mysqli_stmt_execute');
return $refs[0];
}
function db_ce_stmt($query) {
// get all arguments, and discard the first
$args = func_get_args();
array_shift($args);
return db_vce_stmt($query, $args);
}
function db_vquery($query, $args) {
$stmt = db_vce_stmt($query, $args);
if (!($res = mysqli_stmt_get_result($stmt)))
fatal_mysqli('mysqli_stmt_get_result');
if (!mysqli_stmt_close($stmt))
fatal_mysqli('mysqli_stmt_close');
return $res;
}
function db_query($query) {
// get all arguments, and discard the first
$args = func_get_args();
array_shift($args);
return db_vquery($query, $args);
}
function db_direct($query) {
if (!(mysqli_query($GLOBALS['db'], $query))) fatal_mysqli('mysqli_query');
}
function db_vsingle_row($query, $args) {
$res = db_vquery($query.' LIMIT 1', $args);
$row = mysqli_fetch_assoc($res);
mysqli_free_result($res);
return $row;
}
function db_single_row($query) {
// get all arguments, and discard the first
$args = func_get_args();
array_shift($args);
return db_vsingle_row($query, $args);
}
function db_vsingle_field($query, $args) {
$row = db_vsingle_row($query, $args);
if (!is_array($row)) return false; // no row
if (!count($row)) fatal('first element of result row array not set?!?!?');
// reset returns the first element of the array
return reset($row);
}
function db_single_field($query) {
// get all arguments, and discard the first
$args = func_get_args();
array_shift($args);
return db_vsingle_field($query, $args);
}
function db_vexec($query, $args) {
$stmt = db_vce_stmt($query, $args);
if (($affected_rows = mysqli_stmt_affected_rows($stmt)) < 0)
fatal_mysqli('mysqli_affected_rows');
mysqli_stmt_close($stmt);
return $affected_rows;
}
function db_exec($query) {
$args = func_get_args();
array_shift($args);
return db_vexec($query, $args);
}
function db_get_id($id_name, $table) {
$args = func_get_args();
array_shift($args);
array_shift($args);
$argc = count($args);
if ($argc%2) fatal('number of args to db_get_id after $table must be even');
// build insert and select queries
$insert = $id_name.' = NULL';
$select = 'TRUE';
$values = array();
for ($i = 0; $i < $argc; $i += 2) {
$insert .= ', '.$args[$i].' = ?';
$select .= ' AND '.$args[$i].' = ?';
$values[] = &$args[$i+1];
}
db_direct("LOCK TABLES $table WRITE");
$id = db_vsingle_field('SELECT '.$id_name.' FROM '.$table.' WHERE '.$select, $values);
if (!$id) {
if (db_vexec('INSERT INTO '.$table.' SET '.$insert, $values) != 1)
fatal('expected INSERT to affect precisely 1 row, but it did not');
$id = mysqli_insert_id($GLOBALS['db']);
}
db_direct('UNLOCK TABLES');
return $id;
}
function db_get_useragent_id($useragent_string) {
$hash = hash('sha256', $useragent_string);
db_direct('LOCK TABLES useragents WRITE');
$id = db_single_field('SELECT useragent_id FROM useragents WHERE useragent_hash = ?', $hash);
if (!$id) {
if (db_exec('INSERT INTO useragents ( useragent_string, useragent_hash ) VALUES ( ?, ? )', $useragent_string, $hash) != 1)
fatal('expected INSERT to affect precisely 1 row, but it did not');
$id = mysqli_insert_id($GLOBALS['db']);
}
db_direct('UNLOCK TABLES');
return $id;
}
function db_dump_result($res, $show_table_names = 0) {
echo("<table>\n<tr>");
while (($finfo = $res->fetch_field())) {
echo('<th>');
if ($show_table_names) echo($finfo->table.'<br>');
echo($finfo->name.'</th>');
}
while (($row = mysqli_fetch_array($res, MYSQLI_NUM))) {
echo('<tr>');
foreach ($row as $data) {
if ($data === NULL) echo('<td><i>NULL</i></td>');
else echo('<td>'.$data.'</td>');
}
echo("<tr>\n");
}
echo("</table>\n");
// reset result, so that it can be traversed again
mysqli_field_seek($res, 0);
mysqli_data_seek($res, 0);
}