-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyapi.php
130 lines (79 loc) · 2.3 KB
/
myapi.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
header("Content-type: application/json");
$method = $_SERVER['REQUEST_METHOD'];
switch($method){
case 'GET':
#code here
handlegetOperation();
break;
case 'POST':
#code here
$data = json_decode(file_get_contents('php://input'), true);
handlepostOpaerration($data);
break;
case 'PUT':
#code here
$data = json_decode(file_get_contents('php://input'), true);
handlePutOperation($data);
break;
case 'DELETE':
#code here
$data = json_decode(file_get_contents('php://input'), true);
handleDeleteOperation($data);
break;
default:
break;
}
//getOperation.....
function handlegetOperation(){
include "db.php";
$sql = "SELECT * FROM testtable";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows["result"][] = $r; // with result object
// $rows[] = $r; // only array
}
echo json_encode($rows);
} else {
echo '{"result": "No data found"}';
}
}
//postOperation.....
function handlepostOpaerration($data ){
include "db.php";
$name = $data["name"];
$phone = $data["phone"];
$sql = "INSERT INTO testtable(name, phone, datetime) VALUES('$name', '$phone' , NOW())";
if (mysqli_query($conn, $sql)) {
echo '{"result": "Success"}';
} else {
echo '{"result": "Sql error"}';
}
}
//put oparation....
function handlePutOperation($data){
include "db.php";
$id = $data["id"];
$name = $data["name"];
$phone = $data["phone"];
$sql = "UPDATE testtable SET name = '$name', phone = '$phone', datetime = NOW() WHERE id = '$id'";
if (mysqli_query($conn, $sql)) {
echo '{"result": "Success"}';
} else {
echo '{"result": "Sql error"}';
}
}
function handleDeleteOperation($data){
include "db.php";
$id = $data["id"];
$sql = "DELETE FROM testtable WHERE id = $id";
if (mysqli_query($conn, $sql)) {
echo '{"result": "Success"}';
} else {
echo '{"result": "Sql error"}';
}
}
?>