-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
144 lines (123 loc) · 3.61 KB
/
server.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
<?php
// Get the variable
$op = $_REQUEST['op'];
$content = $_REQUEST['content'];
$id = $_REQUEST['id'];
// Choose the operation
switch ($op){
case "getList":
$dbObj = new DBAccess();
$dbObj->getList();
break;
case "addList":
$dbObj = new DBAccess();
$dbObj->addList($content);
break;
case "updateList":
$dbObj = new DBAccess();
$dbObj->updateList($id, $content);
break;
case "removeList":
$dbObj = new DBAccess();
$dbObj->removeList($id);
break;
case "removeAllList":
$dbObj = new DBAccess();
$dbObj->removeAllList();
break;
}
class DBAccess
{
// Database information for local testing
private $dbHost = "localhost"; // address to the database
private $dbUser = "root"; // username
private $dbPass = "root"; // password
// Connect to MySQL database
private function connect(){
// Connect database
$link = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass) or exit(mysql_error());
// Create database if it does not exists
mysql_query("CREATE DATABASE IF NOT EXISTS dbToDoList") or exit(mysql_error());
// Select database
mysql_select_db("dbToDoList");
// Create a table if it does not exists
mysql_query("CREATE TABLE IF NOT EXISTS tbToDoList(
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
content VARCHAR(255) NOT NULL,
UNIQUE(id),
PRIMARY KEY (id)
)") or exit(mysql_error());
return $link;
}
public function getList()
{
$link = $this->connect();
// Query for all the entries from the table and order them based on the id descendingly
$query = "SELECT * FROM tbToDoList ORDER BY id DESC";
$res = mysql_query($query);
$res_array = array();
// fetch all the entires one by one
while($row=mysql_fetch_array($res)){
// put query result in php array
$array = array('id' => $row['id'],
'content' => $row['content']);
// add into the big array
$res_array[] = $array;
}
// Encode the array in JSON and pass back
echo json_encode($res_array);
// close the connection to the database
mysql_close($link);
}
public function addList($content)
{
$link = $this->connect();
// insert the entry to the database
$query = "INSERT INTO tbToDoList(content) VALUES('$content')";
mysql_query($query) or exit(mysql_error());
// query for all the entries
$query = "SELECT * FROM tbToDoList ORDER BY id DESC";
$res = mysql_query($query) or exit(mysql_error());
// get the one just added
$row=mysql_fetch_array($res);
// put query result in php array
$array = array('id' => $row['id'],
'content' => $row['content']);
// Encode the array in JSON and pass back
echo json_encode($array);
// close the connection to the database
mysql_close($link);
}
public function removeList($id)
{
$link = $this->connect();
// delete the entry from database
$query = "DELETE FROM tbToDoList WHERE id ='$id'";
mysql_query($query) or exit(mysql_error());
mysql_close($link);
}
public function removeAllList($id)
{
$link = $this->connect();
// delete the entry from database
$query = "TRUNCATE TABLE tbToDoList";
mysql_query($query) or exit(mysql_error());
// Create a table again
mysql_query("CREATE TABLE IF NOT EXISTS tbToDoList(
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
content VARCHAR(255) NOT NULL,
UNIQUE(id),
PRIMARY KEY (id)
)") or exit(mysql_error());
mysql_close($link);
}
public function updateList($id, $content)
{
$link = $this->connect();
// update the content of entry[id]
$query = "UPDATE tbToDoList SET content='$content' WHERE id ='$id'";
mysql_query($query) or exit(mysql_error());
mysql_close($link);
}
}
?>