This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
db-func.php
161 lines (131 loc) · 3.68 KB
/
db-func.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
<?php // vim:set ts=4 sw=4 sts=4 et:
require_once "html.php";
require_once "config.php";
// Connect to database
if (($db = mysql_connect(DB_SERVER, DB_USER, DB_PASS)) == FALSE) {
echo '<div class="errormsg">Could not connect to database server ' . DB_SERVER . '.</div>';
foot();
exit(1);
}
// Use UTF-8 character set for connection
if (!mysql_set_charset('utf8mb4')) {
echo '<div class="errormsg">Could not set character set.</div>';
foot();
exit(1);
}
// Select database
if (mysql_select_db(DB_NAME, $db) == FALSE) {
echo '<div class="errormsg">Could not select database ' . DB_NAME . '.</div>';
foot();
exit(1);
}
// Query database.
// Error if no result is found.
function query_db($query) {
$result = mysql_query($query);
if ($result == FALSE) {
db_error($query);
}
return $result;
}
// Get one row from database, as an array.
// Error if no result found or if more than 1 row present.
function get_row($query) {
$result = query_db($query);
if (mysql_num_rows($result) != 1) {
db_unexpected($query);
}
$r = mysql_fetch_array($result);
return $r;
}
// Get one row from database, as an array.
// Return null if no result found.
// Error if more than 1 row found.
function get_row_null($query) {
$result = query_db($query);
if (mysql_num_rows($result) == 0) {
return NULL;
}
if (mysql_num_rows($result) != 1) {
db_unexpected($query);
}
$r = mysql_fetch_array($result);
return $r;
}
// Get multiple rows from database, as an array of arrays.
// Return an empty array if no result found.
function get_rows($query) {
$result = query_db($query);
$rows = array();
while ($r = mysql_fetch_array($result)) {
$rows[] = $r;
}
return $rows;
}
// Get multiple rows from database, as an array of associative arrays.
// Return an empty array if no result found.
function get_row_dicts($query) {
$result = query_db($query);
$rows = array();
while ($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
return $rows;
}
// Get an associative array
function get_assoc_array($query, $key_col, $val_col) {
$arr = array();
$result = query_db($query);
while ($r = mysql_fetch_array($result)) {
$arr[$r[$key_col]] = $r[$val_col];
}
return $arr;
}
// Get a single datum from the database.
// Error if no result found
function get_element($query) {
$r = get_row($query);
if ($r == NULL) {
return NULL;
} else {
return $r[0];
}
}
// Get a single datum from the database.
// Return null if no result found
function get_element_null($query) {
$r = get_row_null($query);
if ($r == NULL) {
return NULL;
} else {
return $r[0];
}
}
// Get a column from the database, as an array.
// Return an empty array if no result found
function get_elements($query) {
$result = query_db($query);
$elements = array();
while ($r = mysql_fetch_array($result)) {
$elements[] = $r[0];
}
return $elements;
}
// Check if a query returns a result
function has_result($query) {
$result = query_db($query);
return mysql_num_rows($result) > 0;
}
// On database error, give error and stop.
function db_fail_message($query, $message) {
mysql_query('ROLLBACK');
echo "<div class='errormsg'>$message<br /><code>" . htmlspecialchars($query) . "</code></div>";
foot();
exit(1);
}
function db_error($query) {
db_fail_message($query, "An error has occurred while querying the database. Please try again.");
}
function db_unexpected($query) {
db_fail_message($query, "The number of rows resulting from querying the database was unexpected. Please try again.");
}