-
Notifications
You must be signed in to change notification settings - Fork 0
/
readAllPostgre.php
92 lines (70 loc) · 2.32 KB
/
readAllPostgre.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
<?php
// header("Access-Control-Allow-Origin: *");
// header("Access-Control-Allow-Headers: access");
// header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
// header("Content-Type: application/json; charset=UTF-8");
// header("Access-Control-Allow-Headers: Origin, Content-Type, Access-Control-Allow-Headers,Accept, Authorization, X-Requested-With");
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
if ($_SERVER['REQUEST_METHOD'] !== 'GET') :
http_response_code(405);
echo json_encode([
'success' => 0,
'message' => 'Invalid Request Method. HTTP method should be GET',
]);
exit;
endif;
require 'databasePostgre.php';
// $database = new Database();
// $conn = $database->dbConnection();
global $conn;
// $product_id = null;
// if (isset($_GET['id'])) {
// $product_id = filter_var($_GET['id'], FILTER_VALIDATE_INT, [
// 'options' => [
// 'default' => 'all_products',
// 'min_range' => 1
// ]
// ]);
// }
$query = "SELECT * FROM product";
$result = pg_query($conn, $query);
if (!$result) {
echo "Error when mining data!";
exit;
}
while ($row = pg_fetch_assoc($result)) {
$results[] = $row;
}
http_response_code(200);
echo json_encode([
'success' => 1,
'data' => $results,
]);
// function getAll($tbname)
// {
// global $conn;
// $query = "SELECT * FROM " . $tbname;
// $result = pg_query($conn, $query);
// if (!$result) {
// echo "Error when mining data!";
// exit;
// }
// while ($row = pg_fetch_assoc($result)) {
// $results[] = $row;
// }
// return $results;
// }
?>