forked from HagopA/COMP353
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectToDB.php
158 lines (146 loc) · 5.57 KB
/
ConnectToDB.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
function ConnectToDatabase()
{
$servername = "ryc353.encs.concordia.ca";
$dbname = "ryc353_4";
$username = "ryc353_4";
$password = "353winte";
#Using PDO
try {
$connection = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully <br/>";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
return $connection;
}
#1: Which student(s) is not a member of any team?
function getStudentsNotInATeam()
{
$connection = ConnectToDatabase();
$results = $connection->query("SELECT students.SID FROM students
WHERE students.SID NOT IN (SELECT members.SID FROM members);");
$connection = null;
return $results;
}
#2: For each team, list its members.
function getListOfMembersOnTeams()
{
$connection = ConnectToDatabase();
$results = $connection->query("SELECT teams.TID, students.SID, students.Name FROM teams, students, members
WHERE members.TID = teams.TID AND members.SID = students.SID
ORDER BY teams.TID ASC;");
$connection = null;
return $results;
}
#3: Who was not present in the demo of a team?
function getStudentsNotInDemos()
{
$connection = ConnectToDatabase();
$results = $connection->query("SELECT students.Name from students, teams
WHERE students.SID IN (SELECT members.SID FROM members WHERE members.TID = teams.TID)
AND students.SID NOT IN (SELECT demos.SID FROM demos WHERE demos.TID = teams.TID);");
$connection = null;
return $results;
}
#4: List the teams which have less than four members.
function getTeamsWithLessThanFourMembers()
{
$connection = ConnectToDatabase();
$results = $connection->query("SELECT teams.TID FROM teams WHERE teams.NoOfMembers < 4;");
$connection = null;
return $results;
}
#5: Given a TID, list the names of the members
#$teamID is an integer
function getListOfTeamMembers($teamID)
{
$connection = ConnectToDatabase();
try
{
$results = $connection->query("SELECT students.Name FROM students, members
WHERE students.SID = members.SID AND members.TID = $teamID;");
}
catch(Exception $e)
{
echo "Invalid input";
}
$connection = null;
return $results;
}
#6: Given a date, list all of the teams that have demos on that day.
#$date is formatted YYYY-MM-DD
function getTeamsWhoHaveDemos($date)
{
$connection = ConnectToDatabase();
try
{
$results = $connection->query("SELECT DISTINCT demos.TID FROM demos WHERE demos.Day = '$date';");
}
catch(Exception $e)
{
echo "Invalid input";
}
$connection = null;
return $results;
}
#7: For each teams that is not complete (<4 members), list the TID and its capacity to increase.
function getTeamsCapacityToIncrease()
{
$connection = ConnectToDatabase();
$results = $connection->query("SELECT teams.TID, (4-teams.NoOfMembers) FROM teams WHERE teams.NoOfMembers < 4;");
$connection = null;
return $results;
}
#8: Given a student's name or ID, find their team ID.
#$student is an integer or a string
function getTeamIdByStudent($student)
{
$connection = ConnectToDatabase();
if (is_numeric($student))
{
$student = (int) $student;
$results = $connection->query("SELECT members.TID FROM members WHERE members.SID = $student;");
}
else
{
$student = (string) $student;
$results = $connection->query("SELECT members.TID FROM members, students
WHERE members.SID = students.SID AND students.Name = '$student';");
}
$connection = null;
return $results;
}
#9: Given a student's name or ID, find their teammates names and IDs.
#$student is an integer or a string
function getTeammatesByStudent($student)
{
$connection = ConnectToDatabase();
try
{
if (is_numeric($student))
{
$student = (int) $student;
$results = $connection->query("SELECT members.SID, students.Name FROM members, students
WHERE members.SID = students.SID AND
members.TID IN (SELECT members.TID FROM members WHERE members.SID = $student);");
}
else
{
$student = (string) $student;
$results = $connection->query("SELECT members.SID, students.Name FROM members, students
WHERE members.SID = students.SID AND
members.TID IN (SELECT members.TID FROM members
WHERE members.SID IN (SELECT students.SID from students
WHERE students.Name = '$student'));");
}
}
catch(Exception $e)
{
echo "Invalid input";
}
$connection = null;
return $results;
}
?>