-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.php
More file actions
42 lines (33 loc) · 1.33 KB
/
get.php
File metadata and controls
42 lines (33 loc) · 1.33 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
<?php
include 'src/Connection.php'; // include Connection file
// Headers
header('Access-control-Allow-Origin:*');
header('Content-type:application/json');
header('Access-Control-Allow-Methods:GET');
header('Access-Control-Allow-Headers:Content-Type,Access-Control-Allow-Headers,Authorisation,X-Requested-With');
$table = 'student'; // table name
// Recive data from api url
$data = json_decode(file_get_contents("php://input"));
// Default sql query to read all data from table
$query = $Conn->prepare("SELECT * FROM $table");
if(isset($_GET['id'])) // check if id passed in the parameter
{
// prepare query to get single value corresponding to an student id
$query = $Conn->prepare("SELECT * FROM $table WHERE id = ?");
$query->bind_param("i", $_GET['id']); // bind an integer parameter id into the query
}
if($query->execute()) // execute the query and check if its executed successfully
{
$students = $query->get_result()->fetch_all(MYSQLI_ASSOC); // fetch result as an associative array
}
if(!empty($students)) // check if its return an empty array
{
echo json_encode($students); // print the data into json format
http_response_code(200); // return http status code 200
}
else
{
echo json_encode(["error"=>"no record found"]);
http_response_code(404);
}
?>