-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbHandler.php
More file actions
114 lines (83 loc) · 3.45 KB
/
Copy pathDbHandler.php
File metadata and controls
114 lines (83 loc) · 3.45 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
<?php
/**
* Class to handle all db operations
* This class will have CRUD methods for database tables
*
* @author Goke Odubanjo
*/
class DbHandler {
private $conn;
function __construct($client) {
$this->conn = $client;
}
//create the user
public function createUser($id,$username){
$sql = "INSERT INTO users (user_id,username) VALUES (:id,:username)";
$sth = $this->conn->prepare($sql);
$sth->bindParam("id", $id);
$sth->bindParam("username", $username);
$sth->execute();
}
//check if user exists to avoid duplicates
public function checkUser($id){
$sql = "SELECT count(*) FROM users WHERE user_id = :id";
$sth = $this->conn->prepare($sql);
$sth->bindParam("id", $id);
$sth->execute();
$num_rows = $sth->fetchColumn();
return $num_rows > 0;
}
//inserts messages into db
public function insertMessage($sender,$receiver,$message){
$sql = "INSERT INTO message (message,from_user,to_user,message_datetime) VALUES (:message,:from_user,:to_user,NOW())";
$sth = $this->conn->prepare($sql);
$sth->bindParam("from_user", $sender);
$sth->bindParam("to_user", $receiver);
$sth->bindParam("message", $message);
$sth->execute();
}
//fetch the governor and other info by state
public function getGovernor($state){
$sth = $this->conn->prepare("SELECT * FROM state_governors WHERE state LIKE :state");
$sth->bindParam("state", $state);
$sth->execute();
$gov = $sth->fetchObject();
return $gov;
}
//get all users
public function allUsers(){
$sth = $this->conn->prepare("SELECT * FROM users WHERE username !='BOT' ");
$sth->execute();
$users = $sth->fetchAll();
return $users;
}
//fetch chat from bot to user
public function fetchChat($uid,$bid){
$sth = $this->conn->prepare("SELECT * FROM message WHERE (from_user = :uid AND to_user = :bid ) OR (from_user = :bid AND to_user = :uid) ORDER BY message_datetime");
$sth->bindParam("uid", $uid);
$sth->bindParam("bid", $bid);
$sth->execute();
$gov = $sth->fetchAll();
return $gov;
}
//check if governor exists to avoid duplicates
public function checkGov($id){
$sql = "SELECT count(*) FROM state_governors WHERE state = :id";
$sth = $this->conn->prepare($sql);
$sth->bindParam("id", $id);
$sth->execute();
$num_rows = $sth->fetchColumn();
return $num_rows > 0;
}
//inserts governor lis into db
public function insertGov($state,$gov,$year,$party){
$sql = "INSERT INTO state_governors (state,governor,elected,party) VALUES (:state,:gov,:elected,:party)";
$sth = $this->conn->prepare($sql);
$sth->bindParam("state", $state);
$sth->bindParam("gov", $gov);
$sth->bindParam("elected", $year);
$sth->bindParam("party", $party);
$sth->execute();
}
}
?>