-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.php
More file actions
167 lines (155 loc) · 5.83 KB
/
db.php
File metadata and controls
167 lines (155 loc) · 5.83 KB
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
<?php
class Database {
private $server;
private $username;
private $password;
private $database;
private $connection;
public function __construct($server, $username, $password, $database) {
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
public function connect() {
$this->connection = new PDO("mysql:host=$this->server;dbname=$this->database", $this->username, $this->password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function execute($sql){
try {
$stmt = $this->connection->prepare( $sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
return $rows;
} catch (PDOException $e) {
echo "Query failed: " . $e->getMessage();
}
}
public function getChannelsLogged() {
try {
$stmt = $this->connection->prepare("SELECT DISTINCT(channel) FROM history");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
echo $row['channel'] . "<br>";
}
} catch (PDOException $e) {
echo "Query failed: " . $e->getMessage();
}
}
public function getHistory($channel) {
try {
$stmt = $this->connection->prepare("SELECT * FROM history WHERE channel = :channel");
$stmt->bindParam(':channel', $channel);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
echo "ID: " . $row['id'] . ", chatmsg: " . $row['chatmsg'] . "<br>";
}
} catch (PDOException $e) {
echo "Query failed: " . $e->getMessage();
}
}
public function getCount($channel) {
try {
$stmt = $this->connection->prepare("SELECT count(*) FROM history WHERE channel = :channel");
$stmt->bindParam(':channel', $channel);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
echo "ID: " . $row['id'] . ", chatmsg: " . $row['chatmsg'] . "<br>";
}
} catch (PDOException $e) {
echo "Query failed: " . $e->getMessage();
}
}
public function getEntryCount() {
try {
$stmt = $this->connection->prepare("SELECT count(*) as total FROM history");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
return $rows;
} catch (PDOException $e) {
echo "Query failed: " . $e->getMessage();
}
}
public function addChannel($channel) {
try {
$stmt = $this->connection->prepare("INSERT INTO config (channel) VALUES (:channel)");
$stmt->bindParam(':channel', $channel);
$stmt->execute();
flog('ADDED CHANNEL '.$channel);
return "Channel added successfully.";
} catch (PDOException $e) {
flog("ADD CHANNEL FAIL ". $e->getMessage());
return "Query failed: " . $e->getMessage();
}
}
public function addJoin($username, $channel) {
try {
$stmt = $this->connection->prepare("INSERT INTO visits (username,channel) VALUES (:username, :channel)");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':channel', $channel);
$stmt->execute();
flog('ADDED JOIN '.$channel);
return "Channel added successfully.";
} catch (PDOException $e) {
flog("ADD CHANNEL FAIL ". $e->getMessage());
return "Query failed: " . $e->getMessage();
}
}
public function addChat($author, $channel,$message,$emotes) {
try {
$stmt = $this->connection->prepare("INSERT INTO history (author, channel, message, emotes) VALUES (:author,:channel,:message,:emotes)");
$stmt->bindParam(':author', $author);
$stmt->bindParam(':channel', $channel);
$stmt->bindParam(':message', $message);
$stmt->bindParam(':emotes', $emotes);
$stmt->execute();
return "added successfully.";
} catch (PDOException $e) {
flog("ADD CHAT FAIL ". $e->getMessage());
return "Query failed: " . $e->getMessage();
}
}
public function removeChannel($channel) {
try {
$stmt = $this->connection->prepare("DELETE FROM config WHERE channel = :channel");
$stmt->bindParam(':channel', $channel);
$stmt->execute();
// flog('TERMINATING IRC CONNECTION');
flog("REMOVED CHANNEL ".$channel);
return "Channel removed successfully.";
} catch (PDOException $e) {
flog('REMOVE CHANNEL FAIL '.$e->getMessage());
return "Query failed: " . $e->getMessage();
}
}
public function getChannels() {
try {
$stmt = $this->connection->prepare("SELECT channel FROM config");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $rows;
} catch (PDOException $e) {
echo "Query failed: " . $e->getMessage();
}
}
public function getConfig() {
try {
$stmt = $this->connection->prepare("SELECT channel FROM config");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
return $rows;
} catch (PDOException $e) {
echo "Query failed: " . $e->getMessage();
}
}
}
$server = 'localhost';
$username = 'root';
$password = '@PASSword123';
$database = 'Twitch';
$db = new Database($server, $username, $password, $database);
$db->connect();
?>