-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_data.php
More file actions
40 lines (32 loc) · 959 Bytes
/
fetch_data.php
File metadata and controls
40 lines (32 loc) · 959 Bytes
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
<?php
require 'connect.php';
// Ambil ID dari parameter GET
$id = isset($_GET['id']) ? $_GET['id'] : "";
// Persiapkan statement SQL dengan parameterized query
$query = "SELECT * FROM users WHERE id = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("i", $id); // "i" adalah tipe data integer
// Eksekusi statement
$stmt->execute();
// Ambil hasil kueri
$result = $stmt->get_result();
// Inisialisasi array untuk menyimpan data
$data = [];
// Loop melalui hasil kueri dan tambahkan ke array
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
// Cek apakah data ditemukan
if (empty($data)) {
// Data tidak ditemukan, kirim respons 404 Not Found
http_response_code(404);
echo json_encode(["message" => "Data not found"]);
} else {
// Data ditemukan, kirimkan sebagai respons JSON
header('Content-Type: application/json');
echo json_encode($data);
}
// Tutup statement dan koneksi
$stmt->close();
$con->close();
?>