-
Notifications
You must be signed in to change notification settings - Fork 0
/
getQuestions.php
73 lines (56 loc) · 1.51 KB
/
getQuestions.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
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "123456";
$database = "polltest";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
//echo '<script>console.log("Connection successful")</script>';
echo "Can't connect to question server :<";
exit(1);
}
else {
/*
$time = $_POST['time'];
$sql = "SELECT * FROM questions ORDER BY time DESC LIMIT 1";
$result = mysqli_query($conn, $sql);
$arr = [];
if(mysqli_num_rows($result) != 0) {
$row = mysqli_fetch_assoc($result);
if($row['time'] > $time) {
$arr['id'] = $row['id'];
$arr['name'] = $row['name'];
$arr['question'] = $row['question'];
$arr['time'] = $row['time'];
}
}
*/
$time = 0;
if(isset($_SESSION["lastTime"])) {
$time=$_SESSION["lastTime"];
}
$sql = $conn->prepare('SELECT * FROM questions');
$sql->execute();
$result = $sql->get_result();
$count = 0;
$arr = [];
if(mysqli_num_rows($result) != 0) {
while($row = mysqli_fetch_assoc($result)) {
if($row['time'] > $time) {
$arr[$count]['id'] = $row['id'];
$arr[$count]['name'] = $row['name'];
$arr[$count]['question'] = $row['question'];
$arr[$count]['time'] = $row['time'];
$count++;
}
}
}
if($count > 0) {
$_SESSION["lastTime"] = $arr[$count-1]['time'];
}
echo json_encode($arr);
exit(0);
}
?>