-
Notifications
You must be signed in to change notification settings - Fork 0
/
exams.php
57 lines (54 loc) · 1.92 KB
/
exams.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
<?php
/*
* Name: Amer.Mohammed
* Student ID: 1002254552
* Course code: CSCB20S
* Single PHP file for retrieving all the data from the MySQL server online, that are
* used in assignment 3; Exam timetable webpage.
*/
// Including the config.php file to log in to the mysql server with given username and pass.
include "config.php";
// connect to the database
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
// Die if connection fails.
die("Connection failed: " . mysqli_connect_error());
}
// Request for course_code, if given.
$course_code = $_REQUEST["course"];
// Request for course_id, if given.
$course_id = $_REQUEST["course_id"];
// Select query, based on the request given.
if($course_id != ""){
/* Get the exams course code, section, instructor, date, start time, end time
* from the natural jointed courses and time table where the id is one given
* by the user.
*/
$query = "Select course, section, instructor, date, start, end from courses
natural join time where id=$course_id";
}
else{
/* Get all the columns in the courses table where the parent course is like the
* given by the user.
*/
$query = "Select * from courses where course like '$course_code%'";
}
// Connect and get the result form the provided query.
$get_result = mysqli_query($conn, $query);
// Declare an array to store result
$return_result = array();
while ($row = mysqli_fetch_assoc($get_result)) {
// Store result in declared array
$return_result[] = $row;
}
// If the given query return nothing, die and display HTTP 4040 error code, i.e. page not found.
if($return_result == []){
header("HTTP 404 Page Not Found");
die("Page Not Found; invalid request.");
}else{
// Else json encode the result and print it
print json_encode($return_result);
//close mysql connection.
mysqli_close($conn);
}
?>