-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.inc
159 lines (115 loc) · 4.35 KB
/
common.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
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
<?php
/*==================================================
GONE FISHING
v1.0 April 2001
Nigel Gilbert
====================================================*/
/* functions common to all or many PHP files */
include("defines.inc");
function html_header($title = "Gone Fishing!", $bgcolor = "#006699", $link = "#FFFFFF", $text = "#FFFFFF") {
/* insert standard HTML page header */
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
echo "<HTML lang=\"en\">\n";
echo "<HEAD>\n";
echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">";
echo "<META HTTP-EQUIV=\"expires\" CONTENT=\"now\">\n";
echo "<META HTTP-EQUIV=\"pragma\" CONTENT=\"no-cache\">\n";
echo "<TITLE>$title</TITLE>\n";
echo "</HEAD>\n";
echo "<BODY bgcolor=\"$bgcolor\" text=\"$text\" link=\"$link\" vlink=\"$link\" alink=\"$link\">\n";
}
function html_footer() {
/* display standard HTML footer */
echo "</BODY>\n</HTML>";
}
function alert($message) {
/* put up a modal dialog box with the message */
html_header("Alert");
echo "<SCRIPT> alert('$message'); history.go(-1)</SCRIPT>";
html_footer();
}
function debug($var, $val) {
/* displays the value of the variable, but only if the $debug global is true */
global $debug;
if ($debug) printf("<font color=yellow>%s = %s </font><p>\n", $var, $val);
}
//==================================database interface====================================//
function db_open($database = "fishing") {
/* open the database and trap errors. Returns link to database */
global $db;
$db = @pg_connect("dbname=$database");
if (!$db) db_error("Cannot connect to database: $database");
return $db;
}
function db_write($sql) {
/* carry out an UPDATE or INSERT operation. Assumes db is already connected. */
global $db;
$result = @pg_exec($db, $sql);
if (!$result) db_error("", $sql);
}
function db_error($errormsg="", $sql="") {
/* display an error message and then die */
global $admin_mail, $db;
if (empty($errormsg)) {
$errormsg = pg_errormessage($db);
}
html_header("Database Error", "#FFFFFF", "#000000", "#000000");
echo "<font color=red><H1>Database error</H1>A database error has been encountered:</font><P>\n";
echo $errormsg;
if ($sql) echo "<BR> (while evaluating: $sql)";
mail($admin_mail, "Gone Fishing! database error",
"Database error:\nevaluating $sql\nError is: $errormsg");
html_footer();
exit;
}
class Query {
/* class to send and store result of a query to the database. Assumes the db is already
connected */
public $cursor,
$result,
$row,
$nrows,
$lastSQL;
function error($msg) {
db_error($msg, $this->lastSQL);
}
function query($query) {
/* never returns if there is an error (prints error message and dies) */
global $db;
$this->lastSQL = $query;
$this->result = @pg_exec($db, $query);
if (!$this->result) $this->error("Bad query");
$this->cursor = 0;
$this->nrows = pg_numrows($this->result);
}
function seek($loc) {
/* positions the cursor at $loc, so that the next record to be read is the one at $loc */
$this->cursor = $loc;
}
function next_rec() {
/* extracts an associative array holding the next record retrieved from the DB.
Returns the array or 0 if no more */
if ($this->cursor >= $this->nrows) return 0;
$this->row = pg_fetch_array($this->result, $this->cursor++);
return $this->row;
}
function num_recs() {
/* returns the number of records retrieved from the DB */
return $this->nrows;
}
function last_rec() {
/* set the cursor to the last row in the retrieved array */
$this->cursor = $this->nrows;
}
function prev_rec() {
/* returns the row before the one previously retrieved, or 0 if there isn't one */
if (!$this->cursor) return 0;
$this->row = pg_fetch_array($this->result, --$this->cursor);
return $this->row;
}
function field($fieldname) {
/* return the value in the given field in the current record */
return $this->row[$fieldname];
}
}