-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdatabase.php
More file actions
executable file
·133 lines (115 loc) · 3.82 KB
/
database.php
File metadata and controls
executable file
·133 lines (115 loc) · 3.82 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
<?php
class Db {
protected $con;
private $host = "us-cdbr-azure-west-c.cloudapp.net";
private $user = "ba1eae06b4de68";
private $pwd = "4d6eb4b7";
private $db = "michaelteclogdb";
//Creates a PDO conection & sets error mode to exceptions
public function __construct(){
try {
$this->con = new PDO("mysql:host=$this->host;dbname=$this->db", $this->user, $this->pwd);
$this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
//sets the datab to null
public function disconect() {
$this->con = null;
}
public function createTable() {
try {
$sql = "CREATE TABLE IF NOT EXISTS notes (
id INT(11) AUTO_INCREMENT,
last_modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
content text,
PRIMARY KEY(id)
);";
$this->con->query($sql);
return true;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function dropTable() {
try {
$sql = "DROP TABLE notes;";
$this->con->query($sql);
return true;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function createNote($content) {
try {
$query = $this->con->prepare("INSERT INTO notes (content) VALUES (:content);");
$query->bindParam(':content', $content);
$query->execute();
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function getNotes() {
try{
$query = $this->con->prepare("SELECT * FROM notes ORDER BY last_modified DESC;");
$query->execute();
return $query->fetchAll();
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function getMinId() {
try{
$query = $this->con->prepare("SELECT min(id) FROM notes;");
$query->execute();
return $query->fetch()[0];
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function getMaxId() {
try{
$query = $this->con->prepare("SELECT max(id) FROM notes;");
$query->execute();
return $query->fetch()[0];
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function isValid($id) {
try{
$query = $this->con->prepare("SELECT * FROM notes WHERE id = :id;");
$query->bindParam(':id', $id);
$query->execute();
return count($query->fetchAll()) > 0;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function deleteNote($id) {
try{
$query = $this->con->prepare("DELETE FROM notes WHERE id = :id;");
$query->bindParam(':id', $id);
$query->execute();
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function updateNote($id, $newContent) {
try{
$query = $this->con->prepare("UPDATE notes
SET content = :content,
last_modified = CURRENT_TIMESTAMP
WHERE id = :id");
$query->bindParam(':id', $id);
$query->bindParam(':content', $newContent);
$query->execute();
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
?>