-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.php
111 lines (92 loc) · 5.22 KB
/
API.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
<?php
require_once('config.php');
header("Content-Type: application/json; charset=UTF-8");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_SESSION['userid'])) {
$action = $_POST['action'];
if ($action == 'addFriend') {
$conn = mysqli_connect($servername, $username, $password, $databasename);
// mysqli_set_charset($conn, 'utf8mb4');
$stmt = mysqli_prepare($conn, "CALL checkFriend(?, ?) ");
mysqli_stmt_bind_param($stmt, "ii", $_SESSION['userid'], $_POST['friendid']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (mysqli_num_rows($result) == 0) {
$conn = mysqli_connect($servername, $username, $password, $databasename);
mysqli_set_charset($conn, 'utf8mb4');
$stmt = mysqli_prepare($conn, "CALL addRoom(?, ?) ");
mysqli_stmt_bind_param($stmt, "ii", $_SESSION['userid'], $_POST['friendid']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$outp = mysqli_fetch_all($result, MYSQLI_ASSOC);
echo json_encode($outp);
}
}
if ($action == 'selectTop10Friend') {
$conn = mysqli_connect($servername, $username, $password, $databasename);
mysqli_set_charset($conn, 'utf8mb4');
$stmt = mysqli_prepare($conn, "CALL selectTop10Friend(?) ");
$userid = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : 0;
mysqli_stmt_bind_param($stmt, "i", $userid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$outp = mysqli_fetch_all($result, MYSQLI_ASSOC);
echo json_encode($outp);
}
if ($action == 'updateChat') {
$_SESSION['roomid'] = $_POST['roomid'];
$_SESSION['maxchatid'] = $_POST['maxchatid'];
$conn = mysqli_connect($servername, $username, $password, $databasename);
$stmt = mysqli_prepare($conn, "CALL selectNewestChat(?, ?) ");
$roomid = (isset($_SESSION['roomid'])) ? $_SESSION['roomid'] : 0;
$maxchatid = (isset($_SESSION['maxchatid'])) ? $_SESSION['maxchatid'] : 0;
mysqli_stmt_bind_param($stmt, "ii", $roomid, $maxchatid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$outp = mysqli_fetch_all($result, MYSQLI_ASSOC);
echo json_encode($outp);
}
if ($action == 'updateFriend') {
$_SESSION['maxtime'] = $_POST['maxtime'];
$conn = mysqli_connect($servername, $username, $password, $databasename);
$stmt = mysqli_prepare($conn, "CALL selectNewestFriend(?, ?) ");
$userid = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : 0;
$maxtime = (isset($_SESSION['maxtime'])) ? $_SESSION['maxtime'] : "";
mysqli_stmt_bind_param($stmt, "is", $userid, $maxtime);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$outp = mysqli_fetch_all($result, MYSQLI_ASSOC);
echo json_encode($outp);
}
if ($action == 'searchFriend') {
$conn = mysqli_connect($servername, $username, $password, $databasename);
$stmt = mysqli_prepare($conn, "CALL searchFriend(?, ?) ");
mysqli_stmt_bind_param($stmt, "is", $_SESSION['userid'], $_POST['s']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$outp = mysqli_fetch_all($result, MYSQLI_ASSOC);
echo json_encode($outp);
}
if ($action == 'selectTop10Chat') {
$_SESSION['roomid'] = $_POST['roomid'];
$conn = mysqli_connect($servername, $username, $password, $databasename);
$stmt = mysqli_prepare($conn, "CALL selectTop10Chat(?) ");
$roomid = (isset($_SESSION['roomid'])) ? $_SESSION['roomid'] : 0;
mysqli_stmt_bind_param($stmt, "i", $roomid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$outp = mysqli_fetch_all($result, MYSQLI_ASSOC);
echo json_encode($outp);
}
if ($action == 'addChat') {
$conn = mysqli_connect($servername, $username, $password, $databasename);
$stmt = mysqli_prepare($conn, "CALL addChat(?, ?, ?) ");
mysqli_stmt_bind_param($stmt, "iis", $_POST['roomid'], $_SESSION['userid'], $_POST['chat']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$outp = mysqli_fetch_all($result, MYSQLI_ASSOC);
echo json_encode($outp);
}
}
}
?>