-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetBudget.php
42 lines (36 loc) · 1.33 KB
/
getBudget.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
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json'); // Ensure JSON response
// Database connection details
$dbServer = 'oceanus.cse.buffalo.edu:3306';
$dbUsername = 'schen277';
$dbPassword = '50396261';
$dbName = 'cse442_2024_spring_team_aa_db';
// Create database connection
$conn = new mysqli($dbServer, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($conn->connect_error) {
echo json_encode(['success' => false, 'message' => "Connection failed: " . $conn->connect_error]);
exit;
}
// Check for GET request to fetch data
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// Prepare the SELECT statement to fetch all goals
$sql = "SELECT id, GoalsName, TotalAmount, CurrentBudget FROM newGoal";
$result = $conn->query($sql);
if ($result === false) {
// Handle query error
echo json_encode(['success' => false, 'message' => "Failed to fetch goals: " . $conn->error]);
exit;
} else {
// Fetch all rows as an associative array
$goals = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode(['success' => true, 'goals' => $goals]);
}
} else {
// Handle non-GET requests
echo json_encode(['success' => false, 'message' => 'Invalid request method']);
}
// Close connection
$conn->close();
?>