-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudent_fetch.php
More file actions
66 lines (55 loc) · 1.8 KB
/
Copy pathStudent_fetch.php
File metadata and controls
66 lines (55 loc) · 1.8 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
header('Content-Type: application/json');
// Database connection
$host = 'localhost';
$dbname = 'school_db';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]);
exit();
}
// Check if student_id is provided
if (!isset($_POST['student_id'])) {
echo json_encode(['error' => 'No student ID provided']);
exit();
}
$student_id = $_POST['student_id'];
try {
// Fetch student information
$stmt = $pdo->prepare("SELECT * FROM students WHERE id = :student_id");
$stmt->execute(['student_id' => $student_id]);
$student = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$student) {
echo json_encode(['error' => 'Student not found']);
exit();
}
// Fetch parent information
$stmt = $pdo->prepare("SELECT * FROM parents_contact WHERE student_id = :student_id");
$stmt->execute(['student_id' => $student_id]);
$parent = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$parent) {
echo json_encode(['error' => 'Parent information not found']);
exit();
}
// Return student and parent information
echo json_encode([
'student' => [
'id' => $student['id'],
'name' => $student['name'],
'grade' => $student['grade']
],
'parent' => [
'student_id' => $parent['student_id'],
'parent_name' => $parent['parent name'],
'email' => $parent['email'],
'telephone_number' => $parent['telephone number']
]
]);
} catch(PDOException $e) {
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
}
?>