-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.php.old
199 lines (157 loc) · 5.48 KB
/
query.php.old
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
<?php
/***********************************************
Originally written for the
Zurich Water Game: internet version
Author: Nigel Gilbert and the FIRMA project
--------------------------------------------
query.php
This file contains functions to interface to the
server database
Version 1.0 6 August 2001
Version 1.1 1 September 2001
Version 1.2 20 December 2003
Version 1.3 19 March 2004
Version 1.4 12 July 2004
Version 1.5 20 August 2004
Version 1.6 18 November 2018
**********************************************/
function db_open($database, $user="www", $pw="") {
/* open the database and trap errors. Returns link to database */
global $db;
if ($user and $pw) {
$db = @pg_connect("dbname=$database user=$user password=$pw");
}
elseif ($user and !$pw) {
$db = @pg_connect("dbname=$database user=$user");
}
else {
$db = @pg_connect("dbname=$database");
}
if (!$db) {
echo "<h2><font color=red>Cannot connect to database: $database</font></h2>
<p>This may be because the database server has not been started, or the username
($user) or password ($pw) are incorrect.</p>";
exit;
}
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_write_no_error($sql) {
/* carry out an UPDATE or INSERT operation. Assumes db is already connected. */
global $db;
$result = @pg_exec($db, $sql);
}
function db_error($errormsg="", $sql="") {
/* display an error message and then die */
global $database, $db;
if (empty($errormsg)) $errormsg = pg_last_error($db);
error_log("$errormsg, $database, $sql, " . debug_backtrace());
if (PHP_SAPI === 'cli') print_r(array('msg' => $errormsg, 'database' => $database, 'sql' => $sql));
else display_error(array('msg' => $errormsg, 'database' => $database, 'sql' => $sql));
}
class Query {
/* class to send and store result of a query to the database. Assumes the db is already
connected */
private
$cursor,
$result,
$row,
$nrows,
$lastSQL;
function __construct($query) {
/* never returns if there is an error (prints error message and dies) */
global $db;
$this->lastSQL = $query;
$this->result = @pg_query($db, $query);
if ($this->result === false) $this->error("");
$this->cursor = 0;
if (($this->nrows = pg_numrows($this->result)) > 0)
$this->row = pg_fetch_assoc($this->result, 0);
}
function error($msg) {
db_error($msg, $this->lastSQL);
}
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 first_rec() {
/* set the cursor to the first row in the retrieved array */
$this->cursor = 0;
}
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];
}
function fields() {
/* return the hash of fields and their values for the current record */
return $this->row;
}
}
function db_modify($arr, $key, $table) {
/* $arr is a hash of field names from the $table table.
$key is a single field name, included in $arr
The table is seached for a single record that has the value in $arr for the $key.
If there is none, a new record is inserted with the values in $arr;
otherwise the found record is updated with the values in $arr
if they are diferent form the existing.
Returns -1 for failure, 0 if the update is the same as the existing,
1 if a new record has been added, or 2 if the record has been updated */
if (!isset($arr[$key])) return -1;
$query = new Query("select * from $table where $key=" . $arr[$key]);
if ($query->num_recs() > 1) return -1; // more than 1 record matches
if ($query->num_recs() == 0) { // insert
$sql = "INSERT into $table (";
$vals = "";
$comma = "";
foreach ($arr as $k => $v) {
$sql .= $comma . $k;
$vals .= $comma . $v;
$comma = ", ";
}
$sql .= ") VALUES ($vals)";
db_write($sql);
return 1;
}
// update? check if update data is different from existing
$changed = false;
foreach ($arr as $k => $v) {
if ($v != prepare($query->field($k))) $changed = true;
}
if (!$changed) return 0;
$sql = "UPDATE $table SET ";
$comma ="";
foreach ($arr as $k => $v) {
$sql .= "$comma$k=$v";
$comma = ", ";
}
$sql .= " where $key=" . $arr[$key];
db_write($sql);
return 2;
}