-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseConnection.php
105 lines (94 loc) · 3.8 KB
/
DatabaseConnection.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
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
class Database {
private $host = "localhost";
private $db_name = "uagc_studentdatabase";
private $username = "Michael.Brassfield"; //Look into setting up a configuration file
private $password = "1234";
private $con;
// Creates connection when object is created
public function __construct() {
$this->con = $this->getConnection();
}
// Function to create database connection
private function getConnection() {
$this->con = null;
try {
$this->con = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
}
return $this->con;
}
//Method to check connection status
public function checkConnection() {
return $this->con !== null;
}
// Function to execute select queries
public function executeSelectQuery($sql, $params = []) {
try {
$stmt = $this->con->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $exception) {
echo "Error: " . $exception->getMessage();
return false;
}
}
// Function to alter or change database
public function executeQuery($sql, $params = []) {
try {
$stmt = $this->con->prepare($sql);
$stmt->execute($params);
return $stmt->rowCount();
} catch(PDOException $exception) {
echo "Error: " . $exception->getMessage();
return false;
}
}
//Backend fucntions for course registration and managment
//Fetch all avalible courses
public function getCourses() {
$sql = "SELECT * FROM tblcourses";
return $this->executeSelectQuery($sql);
}
//List registred classes for student
public function getRegisteredCourses($studentId, $semester) {
$sql = "SELECT c.course_id, c.course_code, c.course_name, c.instructor_name, c.credits
FROM tblregistrations r
JOIN tblcourses c ON r.course_id = c.course_id
WHERE r.student_id = :student_id AND r.semester = :semester";
$params = ['student_id' => $studentId, 'semester' => $semester];
$result = $this->executeSelectQuery($sql, $params);
if ($result === false) {
// Log error or handle it based on your application's needs
error_log("Failed to fetch registered courses for student_id: $studentId, semester: $semester");
return [];
}
// Return the result, which should be an array of courses
return $result;
}
//Register a student for a course
public function registerCourse($studentId, $courseId, $semester) {
$sql = "INSERT INTO tblregistrations (student_id, course_id, semester)
VALUES (:student_id, :course_id, :semester)";
$params = [
'student_id' => $studentId,
'course_id' => $courseId,
'semester' => $semester,
];
return $this->executeQuery($sql, $params);
}
//Delete a class from a student's schedule
public function deleteCourse($studentId, $courseId, $semester) {
$sql = "DELETE FROM tblregistrations
WHERE student_id = :student_id AND course_id = :course_id AND semester = :semester";
$params = [
'student_id' => $studentId,
'course_id' => $courseId,
'semester' => $semester
];
return $this->executeQuery($sql, $params);
}
}
?>