-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_all.php
61 lines (46 loc) · 1.91 KB
/
get_all.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
<?php
/*
Code description only in this file
Symbol: CHn means location of code use find feature in your editor will be easy to find, CHn->CHn means must look at both code
To make it work on your project config these: CH1, CH2, CH3, CH4
*/
// set content type in this file to application json this function return nothing
header("Content-Type: application/json");
/// require file if file is not import yet and will stop if failed to import
require_once("connection.php");
/// include will import file but it will not stop code excution if there are erro but it will just show warning include(), include_once() require is more logical than include function
// CH1
// sql statement
$query = "SELECT * FROM movie";
// prepara statement
$query = $connection->prepare($query);
// rsponse infomation
$response = array();
// this will return object if not will be erro (erro happen bc sql statement or connection)
if($query->execute()) {
// set erro status in response in this case is no erro
$response["error"] = false;
// get result as array
$result = $query->get_result();
// CH2->CH3
// create array to store data from data base
$movies = array();
// reture associative array ( array that have key) ["key" => value]
while ($row = $result->fetch_assoc()) {
// CH3->CH4
$movies[] = $row;
}
// CH4
// add movies data to reponse
$response["movies"] = $movies;
// massage to describe response status
$response["massage"] = "Response Succesfully";
$response["response_code"] = 200;
} else {
$response["erro"] = true;
$response["massage"] = "Response Failed";
$response["response_code"] = 400;
}
// convert data to json
echo json_encode($response);
?>