-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbController.php
More file actions
91 lines (84 loc) · 2.82 KB
/
dbController.php
File metadata and controls
91 lines (84 loc) · 2.82 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
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
<?php
require_once "connect.php";
require_once "listElement.php";
class DbController
{
public static function InsertJSON(string $JSON, int $bitRate,
int $rows, string $author, string $description, string $ip)
{
try
{
$connection = Connector::GetConnection();
if (!($connection->query(
sprintf("INSERT INTO published (author, description, map_code, map_rows, bit_rate, ip) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')",
mysqli_real_escape_string($connection, $author),
mysqli_real_escape_string($connection, $description),
mysqli_real_escape_string($connection, $JSON),
mysqli_real_escape_string($connection, $rows),
mysqli_real_escape_string($connection, $bitRate),
mysqli_real_escape_string($connection, $ip)))))
{
throw new Exception($connection->error);
}
$connection->close();
return true;
}
catch (Exception $e)
{
return "";
}
}
public static function GetJSON(int $id)
{
try
{
$connection = Connector::GetConnection();
if (!($result = ($connection->query(
sprintf("SELECT map_code FROM published WHERE map_id='%s'",
mysqli_real_escape_string($connection, $id))))))
{
throw new Exception($connection->error);
}
$connection->close();
if ($result->num_rows != 1)
{
$result->free();
return "";
}
$JSON = $result->fetch_assoc()['map_code'];
$result->free();
return $JSON;
}
catch (Exception $e)
{
return "";
}
}
public static function GetList($keyword)
{
$connection = Connector::GetConnection();
if ($keyword == false)
{
$query = sprintf("SELECT map_id, author, description FROM published WHERE 1");
}
else
{
$keyword = mysqli_real_escape_string($connection, $keyword);
$query = "SELECT map_id, author, description FROM published WHERE author LIKE '%$keyword%' OR description LIKE '%$keyword%'";
}
if (!($result = ($connection->query($query))))
{
throw new Exception($connection->error);
}
$connection->close();
$list = [];
for ($i = 0; $i < $result->num_rows; $i++)
{
$res = $result->fetch_assoc();
$elem = new ListElement($res['map_id'], $res['author'], $res['description']);
array_push($list, $elem);
}
$result->free();
return json_encode($list);
}
}