-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.php
83 lines (67 loc) · 1.92 KB
/
model.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
<?php
function openDatabaseConnection() {
$base_address = "localhost";
$base_username = "root";
$base_pass = "";
$base_name = "aljosas_blog";
$sql = new mysqli($base_address,$base_username,$base_pass,$base_name);
if ($sql -> connect_errno) {
echo "Failed to connect to MySQL: " . $sql -> connect_error;
exit();
}
return $sql;
}
function closeDatabaseConnection($sql) {
$sql->close();
}
function returnBlogs($limit) {
$sql = openDatabaseConnection();
if ($limit != null && is_int($limit)) {
$query = $sql->prepare("SELECT id, naslov, vsebina FROM blogs ORDER BY id DESC LIMIT ?");
$query->bind_param("i", $limit);
} else {
$query = $sql->prepare("SELECT id, naslov, vsebina FROM blogs ORDER BY id DESC LIMIT 3");
}
$query->execute();
$result = $query->get_result();
if ($result->num_rows > 0) {
$blogs = [];
while ($row = $result->fetch_assoc()) {
$blogs[] = [
'id' => $row['id'],
'title' => $row['naslov'],
'context' => $row['vsebina']
];
}
closeDatabaseConnection($sql);
return $blogs;
} else {
closeDatabaseConnection($sql);
return false;
}
}
function returnBlog($id) {
$sql = openDatabaseConnection();
if(!is_int($id)) {
$id = 1;
}
if($id != null && is_int($id)){
$query = $sql->prepare("SELECT naslov, vsebina FROM blogs WHERE id = ?");
$query->bind_param("i", $id);
}
$query->execute();
$result = $query->get_result();
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$blog = [
'title' => $row['naslov'],
'context' => $row['vsebina']
];
closeDatabaseConnection($sql);
return $blog;
} else {
closeDatabaseConnection($sql);
return false;
}
}
?>