-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAO.php
112 lines (86 loc) · 3.38 KB
/
DAO.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
<?php
class DAO {
const server = "localhost";
const username = "root";
const password = "localhost";
const dbname = "phoneserverdb";
public function insertIntoTable($sender, $receiver, $message, $contet = null )
{
$conn = new mysqli(self::server, self::username, self::password, self::dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO `messages`(`Sender`, `Receiver`, `Message`, `Content`) VALUES('$sender','$receiver', '$message', '$contet')";
if (mysqli_query($conn, $sql)) {
//echo "New message created successfully";
} else {
// echo "Error: " . $sql . " " . $conn->error;
}
$conn->close();
}
public function findReceiver($receiver)
{
$res = array();
$conn = new mysqli(self::server, self::username, self::password, self::dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `Id`, `Sender`, `Receiver`, `Message`, `Content` FROM messages WHERE `Receiver`='$receiver' LIMIT 1";
$result = mysqli_query($conn,$sql);
if (mysqli_query($conn, $sql)) {
while($row = $result->fetch_assoc())
{
$res['id'] = $row['Id'];
$res['sender'] = $row['Sender'];
$res['receiver'] = $row['Receiver'];
$res['message'] = $row['Message'];
$res['content'] = $row['Content'];
}
}
$conn->close();
return $res;
}
public function deleteByID($id)
{
$conn = new mysqli(self::server, self::username, self::password, self::dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM messages WHERE `Id`='$id'";
if(mysqli_query($conn, $sql)){
//echo "Records were deleted successfully.";
} else{
// echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
$conn->close();
}
public function checkTimeout()
{
$conn = new mysqli(self::server, self::username, self::password, self::dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE `messages` SET `Message`='missed' WHERE TIMESTAMPDIFF(SQL_TSI_MINUTE,`RegDate`,CURRENT_TIMESTAMP)>1 AND `Message`='call';";
if (mysqli_query($conn, $sql)) {
//echo "New message created successfully";
} else {
// echo "Error: " . $sql . " " . $conn->error;
}
$conn->close();
}
public function deleteOldMessages()
{
$conn = new mysqli(self::server, self::username, self::password, self::dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM messages WHERE TIMESTAMPDIFF(SQL_TSI_MINUTE,`RegDate`,CURRENT_TIMESTAMP)>3;";
if (mysqli_query($conn, $sql)) {
//echo "New message created successfully";
} else {
// echo "Error: " . $sql . " " . $conn->error;
}
$conn->close();
}
}
?>