-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_util.inc
79 lines (68 loc) · 1.6 KB
/
db_util.inc
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
<?php
// MySQL code for Librarian DB access
$link = null;
$db_error = '';
// connect to a DB with the given parameters
//
function init_db($config) {
global $link;
$link = @mysqli_connect(
$config->db_host, $config->db_user, $config->db_passwd, $config->db_name
);
return $link;
}
// get ID of last insert
//
function insert_id() {
global $link;
return mysqli_insert_id($link);
}
// get string describing last error
//
function db_error() {
global $link;
return mysqli_error($link);
}
function enum_general($query) {
global $link;
$r = mysqli_query($link, $query);
$items = array();
while ($f = mysqli_fetch_object($r)) {
$items[] = $f;
}
mysqli_free_result($r);
return $items;
}
// enumerate a table
//
function enum($table, $clause=null) {
if (!$clause) $clause = 'TRUE';
$query = "select * from $table where $clause";
return enum_general($query);
}
function table_count($table, $clause) {
global $link;
if (!$clause) $clause = 'TRUE';
$query = "select count(*) as total from $table where $clause";
$r = mysqli_query($link, $query);
$x = mysqli_fetch_object($r);
mysqli_free_result($r);
return $x->total;
}
// look up record from table with given ID
//
function lookup_id($table, $id) {
global $link;
$query = "select * from $table where id=$id";
$r = mysqli_query($link, $query);
$source = mysqli_fetch_object($r);
mysqli_free_result($r);
return $source;
}
// return # of rows affected by last query
//
function affected_rows() {
global $link;
return $link->affected_rows;
}
?>