-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
253 lines (219 loc) · 8.68 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
require('credentials.php');
class DB {
private $mysqli;
/* Constructor: setup connection */
public function __construct($host, $user, $pass, $database) {
$this->mysqli = new mysqli($host, $user, $pass, $database);
$this->mysqli->set_charset("utf8");
if($this->mysqli->connect_errno) {
throw new Exception('Connect Error: '.$this->mysqli->connect_errno);
}
}
// public function titlesOfEvents() {
// $query = "SELECT id, title, created_at from events order by created_at desc limit 5";
// if(!($stmt = $this->mysqli->prepare($query))) {
// throw new Exception('DB Error: '.$this->mysqli->error);
// }
// if(!$stmt->execute()) {
// throw new Exception('DB Error: '.$this->mysqli->error);
// }
// $stmt->bind_result($id, $title, $date);
// $items = array();
// while($stmt->fetch()) {
// $items[] = array(
// 'id'=>$id,
// 'title'=>$title,
// 'date'=>$date,
// );
// }
// $stmt->close();
// return $items;
// }
public function getEventById($event_id) {
$query = "SELECT title, content, created_at from events where id = ?";
if(!($stmt = $this->mysqli->prepare($query))) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
$stmt->bind_param('s', $event_id);
if(!$stmt->execute()) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
$stmt->bind_result($title, $content, $date);
while($stmt->fetch()) {
$item = array(
'title'=>$title,
'content'=>$content,
'date'=>$date,
);
}
$stmt->close();
return $item;
}
public function getPika() {
// Step 1. Prepare the SQL query
$query = "SELECT nickname, success, remain_time, hits_score, date(reg_date)
from pika_score
order by success desc, remain_time desc, hits_score desc, reg_date desc";
// Step 2. Prepare the mysqli_stmt object (stmt)
if(!($stmt = $this->mysqli->prepare($query))) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
if(!$stmt->execute()) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
$stmt->bind_result($nickname, $success, $remain_time, $hits_score, $d);
$items = array();
while($stmt->fetch()) {
$items[] = array(
'nickname'=>$nickname,
'success'=>$success,
'remain_time'=>$remain_time,
'hits_score'=>$hits_score,
'date'=>$d,
);
}
$stmt->close();
// Step 6. Return the selected $items to the function caller
return $items;
}
public function newPika($nickname, $success, $remain_time, $hits_score) {
// Step 1. Prepare the SQL query
$query = "insert into pika_score(nickname, success, remain_time, hits_score, reg_date) values (?, ?, ?, ?, CURRENT_TIMESTAMP)";
// Step 2. Prepare the mysqli_stmt object (stmt)
if(!($stmt = $this->mysqli->prepare($query))) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
$stmt->bind_param('ssss', $nickname, $success, $remain_time, $hits_score);
// Step 3. Execute the statement
if(!$stmt->execute()) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
// Step 5. Close the connection
$stmt->close();
}
public function insertPhiChat($c) {
// Step 1. Prepare the SQL query
$query = "insert into phi_chats(phi_id, content, reg_date) values (?, ?, CURRENT_TIMESTAMP)";
// Step 2. Prepare the mysqli_stmt object (stmt)
if(!($stmt = $this->mysqli->prepare($query))) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
$i = mt_rand(1,13);
$stmt->bind_param('ss', $i, $c);
// Step 3. Execute the statement
if(!$stmt->execute()) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
// Step 5. Close the connection
$stmt->close();
}
public function getPhiChat() {
// Step 1. Prepare the SQL query
$query = "SELECT PC.content, PC.src, PI.img_src, PI.desc
from phi_chats PC, phi_info PI
where PC.phi_id = PI.id
order by PC.reg_date desc";
// Step 2. Prepare the mysqli_stmt object (stmt)
if(!($stmt = $this->mysqli->prepare($query))) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
// Step 3. Execute the statement
if(!$stmt->execute()) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
// Step 4. Retrieve the result and put them in the $items array
$stmt->bind_result($content, $src, $img_src, $description);
$items = array();
while($stmt->fetch()) {
$items[] = array('content'=>$content,
'src'=>$src,
'img_src'=>$img_src,
'description'=>$description,
);
}
// Step 5. Close the connection
$stmt->close();
// Step 6. Return the selected $items to the function caller
return $items;
}
public function insertVisitorLog($t, $c) {
// Step 1. Prepare the SQL query
$query = "insert into visitor_log (title, content, reg_date) values (?, ?, CURRENT_TIMESTAMP)";
// Step 2. Prepare the mysqli_stmt object (stmt)
if(!($stmt = $this->mysqli->prepare($query))) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
$stmt->bind_param('ss', $t, $c);
// Step 3. Execute the statement
if(!$stmt->execute()) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
// Step 5. Close the connection
$stmt->close();
}
public function getVisitorLogs() {
// Step 1. Prepare the SQL query
$query = "SELECT title, content, reg_date from visitor_log order by reg_date desc";
// Step 2. Prepare the mysqli_stmt object (stmt)
if(!($stmt = $this->mysqli->prepare($query))) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
// Step 3. Execute the statement
if(!$stmt->execute()) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
// Step 4. Retrieve the result and put them in the $items array
$stmt->bind_result($title, $content, $d);
$items = array();
while($stmt->fetch()) {
$items[] = array('title'=>$title,
'content'=>$content,
'date'=>$d);
}
// Step 5. Close the connection
$stmt->close();
// Step 6. Return the selected $items to the function caller
return $items;
}
public function getDailyLeetCode($date) {
$query = "SELECT url from daily_problems where reg_date = ?";
if(!($stmt = $this->mysqli->prepare($query))) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
$stmt->bind_param('s', $date);
if(!$stmt->execute()) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
$stmt->bind_result($url);
$items = array();
while($stmt->fetch()) {
$items[] = $url;
}
$stmt->close();
return $items[0];
}
public function getDailyLeetCodeList() {
$query = "SELECT url, reg_date from daily_problems order by reg_date";
if(!($stmt = $this->mysqli->prepare($query))) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
if(!$stmt->execute()) {
throw new Exception('DB Error: '.$this->mysqli->error);
}
$stmt->bind_result($url, $date);
$items = array();
while($stmt->fetch()) {
$items[] = array('url'=>$url,
'date'=>$date);
}
$stmt->close();
return $items;
}
};
// Create a DB object $db. We will use $db to connect to database in catalog.php
$db = new DB($credentials['host'],
$credentials['user'],
$credentials['pass'],
$credentials['database']);
?>