- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapis.php
More file actions
41 lines (35 loc) · 1.04 KB
/
apis.php
File metadata and controls
41 lines (35 loc) · 1.04 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
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tetris";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['username'])){
if(!empty($_POST['username'])){
$sql = "INSERT INTO user_score (username, score) VALUES ('" . $_POST['username'] . "', " . $_POST['score'] . ")";
$conn->query($sql);
}
} else {
$sql = "SELECT * FROM user_score ORDER BY score DESC;";
$result = $conn->query($sql);
$arr = array();
// output data of each row
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
array_push($arr,
array(
"username" => $row["username"],
"score" => $row["score"],
"datetime" => $row["datetime"]
));
}
}
echo json_encode($arr);
}
$conn->close();
exit;