Skip to content

Commit c2dc36e

Browse files
TouhidTouhid
Touhid
authored and
Touhid
committed
init
1 parent b63de2c commit c2dc36e

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

db.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
$servername = "localhost";
4+
$username = "root";
5+
$password = "";
6+
$dbname = "my_api";
7+
8+
// Create connection
9+
$conn = new mysqli($servername, $username, $password, $dbname);
10+
11+
//Check connection
12+
// if ($conn->connect_error) {
13+
// die("Connection failed: " . $conn->connect_error);
14+
// }
15+
// echo "Connected successfully";
16+
17+
18+
?>

php_api_demo.php

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
4+
header('Content-Type: application/json');
5+
6+
$method = $_SERVER['REQUEST_METHOD'];
7+
8+
switch ($method) {
9+
10+
case 'POST': // create data
11+
12+
$data = json_decode(file_get_contents('php://input'), true); // true means you can convert data to array
13+
// print_r($data);
14+
postOperation($data);
15+
16+
break;
17+
18+
case 'GET': // read data
19+
getOperation();
20+
break;
21+
22+
case 'PUT': // update data
23+
$data = json_decode(file_get_contents('php://input'), true); // true means you can convert data to array
24+
putOperation($data);
25+
break;
26+
27+
case 'DELETE': // delete data
28+
$data = json_decode(file_get_contents('php://input'), true); // true means you can convert data to array
29+
deleteOperation($data);
30+
break;
31+
32+
default:
33+
print('{"result": "Requested http method not supported here."}');
34+
35+
}
36+
37+
38+
39+
// functions
40+
function putOperation($data){
41+
include "db.php";
42+
43+
$id = $data["id"];
44+
$name = $data["name"];
45+
$phone = $data["phone"];
46+
$area = $data["address"]["area"];
47+
$road = $data["address"]["road"];
48+
$fullAddress = $area . ", " . $road;
49+
50+
$sql = "UPDATE demo_api SET name = '$name', phone = '$phone', address = '$fullAddress', exe_time = NOW() WHERE id = '$id'";
51+
52+
if (mysqli_query($conn, $sql) or die()) {
53+
echo '{"result": "Success"}';
54+
} else {
55+
echo '{"result": "Sql error"}';
56+
}
57+
58+
}
59+
60+
function getOperation(){
61+
62+
include "db.php";
63+
64+
$sql = "SELECT * FROM demo_api";
65+
66+
$result = mysqli_query($conn, $sql);
67+
68+
if (mysqli_num_rows($result) > 0) {
69+
// output data of each row
70+
$rows = array();
71+
while($r = mysqli_fetch_assoc($result)) {
72+
$rows["result"][] = $r; // with result object
73+
// $rows[] = $r; // only array
74+
}
75+
echo json_encode($rows);
76+
77+
} else {
78+
echo '{"result": "No data found"}';
79+
}
80+
81+
}
82+
83+
function postOperation($data){
84+
// print_r($data);
85+
86+
include "db.php";
87+
88+
$name = $data["name"];
89+
$phone = $data["phone"];
90+
$area = $data["address"]["area"];
91+
$road = $data["address"]["road"];
92+
$fullAddress = $area . ", " . $road;
93+
94+
$sql = "INSERT INTO demo_api(name, phone, address, exe_time) VALUES('$name', '$phone', '$fullAddress', NOW())";
95+
96+
if (mysqli_query($conn, $sql)) {
97+
echo '{"result": "Success"}';
98+
} else {
99+
echo '{"result": "Sql error"}';
100+
}
101+
102+
103+
104+
}
105+
106+
function deleteOperation($data){
107+
108+
include "db.php";
109+
110+
$id = $data["id"];
111+
112+
$sql = "DELETE FROM demo_api WHERE id = $id";
113+
114+
if (mysqli_query($conn, $sql)) {
115+
echo '{"result": "Success"}';
116+
} else {
117+
echo '{"result": "Sql error"}';
118+
}
119+
120+
}
121+
122+
123+
?>

0 commit comments

Comments
 (0)