Skip to content

Commit 0295ba8

Browse files
authored
Merge pull request #127 from ComputerScienceHouse/master
Update dev with the mysqli commits.
2 parents a58ccde + 2602fc7 commit 0295ba8

File tree

8 files changed

+86
-72
lines changed

8 files changed

+86
-72
lines changed

api/entity.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
AND s.status != 'X'
4646
GROUP BY c.id
4747
ORDER BY course";
48-
$result = mysql_query($query);
48+
$result = $dbConn->query($query);
4949
if(!$result) {
50-
die(json_encode(array("error" => "mysql", "msg" => mysql_error())));
50+
die(json_encode(array("error" => "mysql", "msg" => $dbConn->error)));
5151
}
5252

5353
// Collect the courses and turn it into a json
5454
$courses = array();
55-
while($course = mysql_fetch_assoc($result)) {
55+
while($course = $result->fetch_assoc()) {
5656
$courses[] = array(
5757
"id" => $course['id'],
5858
"course" => $course['course'],
@@ -98,14 +98,14 @@
9898
AND number IS NOT NULL
9999
ORDER BY id";
100100
}
101-
$result = mysql_query($query);
101+
$result = $dbConn->query($query);
102102
if(!$result) {
103-
die(json_encode(array("error" => "mysql", "msg" => mysql_error())));
103+
die(json_encode(array("error" => "mysql", "msg" => $dbConn->error)));
104104
}
105105

106106
// Collect the departments and turn it into a json
107107
$departments = array();
108-
while($department = mysql_fetch_assoc($result)) {
108+
while($department = $result->fetch_assoc()) {
109109
$departments[] = array(
110110
"id" => $department['id'],
111111
"title" => $department['title'],
@@ -122,14 +122,14 @@
122122
// REQUEST FOR LIST OF SCHOOLS /////////////////////////////////////
123123
// Query for the schools
124124
$query = "SELECT `id`, `number`, `code`, `title` FROM schools";
125-
$result = mysql_query($query);
125+
$result = $dbConn->query($query);
126126
if(!$result) {
127127
die(json_encode(array("error" => "database", "msg" => "The list of schools could not be retrieved at this time.")));
128128
}
129129

130130
// Build an array of schools
131131
$schools = array();
132-
while($school = mysql_fetch_assoc($result)) {
132+
while($school = $result->fetch_assoc()) {
133133
$schools[] = $school;
134134
}
135135

@@ -154,14 +154,14 @@
154154
$query = "SELECT id, number AS code, title FROM schools WHERE number IS NOT NULL ORDER BY number";
155155
}
156156
// Query for the schools
157-
$result = mysql_query($query);
157+
$result = $dbConn->query($query);
158158
if(!$result) {
159159
die(json_encode(array("error" => "database", "msg" => "The list of schools could not be retrieved at this time.")));
160160
}
161161

162162
// Build an array of schools
163163
$schools = array();
164-
while($school = mysql_fetch_assoc($result)) {
164+
while($school = $result->fetch_assoc()) {
165165
$schools[] = $school;
166166
}
167167

@@ -186,14 +186,14 @@
186186
WHERE s.course = '{$_POST['course']}'
187187
AND s.status != 'X'
188188
ORDER BY c.course, s.section";
189-
$sectionResult = mysql_query($query);
189+
$sectionResult = $dbConn->query($query);
190190
if(!$sectionResult) {
191-
die(json_encode(array("error" => "mysql", "msg" => mysql_error())));
191+
die(json_encode(array("error" => "mysql", "msg" => $dbConn->error)));
192192
}
193193

194194
// Collect the sections and their times, modify the section inline
195195
$sections = array();
196-
while($section = mysql_fetch_assoc($sectionResult)) {
196+
while($section = $sectionResult->fetch_assoc()) {
197197
$section['times'] = array();
198198

199199
// Set the course title depending on its section title
@@ -216,12 +216,12 @@
216216
JOIN buildings AS b ON b.number=t.building
217217
WHERE t.section = '{$section['id']}'
218218
ORDER BY day, start";
219-
$timeResult = mysql_query($query);
219+
$timeResult = $dbConn->query($query);
220220
if(!$timeResult) {
221-
die(json_encode(array("error" => "mysql", "msg" => mysql_error())));
221+
die(json_encode(array("error" => "mysql", "msg" => $dbConn->error)));
222222
}
223223

224-
while($time = mysql_fetch_assoc($timeResult)) {
224+
while($time = $timeResult->fetch_assoc()) {
225225
$timeOutput = array(
226226
'start' => $time['start'],
227227
'end' => $time['end'],

api/generate.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,14 @@ function pruneSpecialCourses($schedules, $courseGroups) {
353353
// Close it up and provide order
354354
$query .= " ORDER BY c.course, s.section";
355355

356-
$result = mysql_query($query);
356+
$result = $dbConn->query($query);
357357
if(!$result) {
358358
die(json_encode(array("error" => "mysql", "msg" => "A database error occurred while searching for {$course}")));
359359
}
360-
if(mysql_num_rows($result) == 0) { continue; }
360+
if($result->num_rows == 0) { continue; }
361361

362362
// Fetch all the results and append them to the list
363-
while($row = mysql_fetch_assoc($result)) {
363+
while($row = $result->fetch_assoc()) {
364364
$courseOptions[] = getCourseBySectionId($row['id']);
365365
}
366366
}

api/schedule.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ function icalFormatTime($time) {
3030

3131
function generateIcal($schedule) {
3232
// Globals
33-
global $HTTPROOTADDRESS;
33+
global $HTTPROOTADDRESS, $dbConn;
3434

3535
// We need to lookup the information about the quarter
36-
$term = mysql_real_escape_string($schedule['term']);
36+
$term = $dbConn->real_escape_string($schedule['term']);
3737
$query = "SELECT start, end, breakstart, breakend FROM quarters WHERE quarter='{$term}'";
38-
$result = mysql_query($query);
39-
$term = mysql_fetch_assoc($result);
38+
$result = $dbConn->query($query);
39+
$term = $result->fetch_assoc();
4040
$termStart = strtotime($term['start']);
4141
$termEnd = date("Ymd", strtotime($term['end']));
4242

@@ -103,18 +103,20 @@ function generateIcal($schedule) {
103103
}
104104

105105
function getScheduleFromId($id) {
106+
global $dbConn;
107+
106108
// Query to see if the id exists, if we can update the last accessed time,
107109
// then the id most definitely exists.
108110
$query = "UPDATE schedules SET datelastaccessed = NOW() WHERE id={$id}";
109-
$result = mysql_query($query);
111+
$result = $dbConn->query($query);
110112

111113
$query = "SELECT startday, endday, starttime, endtime, building, `quarter`, CAST(`image` AS unsigned int) AS `image` FROM schedules WHERE id={$id}";
112114

113-
$result = mysql_query($query);
115+
$result = $dbConn->query($query);
114116
if(!$result) {
115117
return NULL;
116118
}
117-
$scheduleInfo = mysql_fetch_assoc($result);
119+
$scheduleInfo = $result->fetch_assoc();
118120
if(!$scheduleInfo) {
119121
return NULL;
120122
}
@@ -133,18 +135,18 @@ function getScheduleFromId($id) {
133135

134136
// It exists, so grab all the courses that exist for this schedule
135137
$query = "SELECT section FROM schedulecourses WHERE schedule = {$id}";
136-
$result = mysql_query($query);
137-
while($course = mysql_fetch_assoc($result)) {
138+
$result = $dbConn->query($query);
139+
while($course = $result->fetch_assoc()) {
138140
$schedule[] = getCourseBySectionId($course['section']);
139141
}
140142

141143
// Grab all the non courses that exist for this schedule
142144
$query = "SELECT * FROM schedulenoncourses WHERE schedule = $id";
143-
$result = mysql_query($query);
145+
$result = $dbConn->query($query);
144146
if(!$result) {
145-
echo mysql_error();
147+
echo $dbConn->error();
146148
}
147-
while($nonCourseInfo = mysql_fetch_assoc($result)) {
149+
while($nonCourseInfo = $result->fetch_assoc()) {
148150
$schedule[] = array(
149151
"title" => $nonCourseInfo['title'],
150152
"courseNum" => "non",
@@ -170,12 +172,14 @@ function getScheduleFromId($id) {
170172
}
171173

172174
function getScheduleFromOldId($id) {
175+
global $dbConn;
176+
173177
$query = "SELECT id FROM schedules WHERE oldid = '{$id}'";
174-
$result = mysql_query($query);
175-
if(!$result || mysql_num_rows($result) != 1) {
178+
$result = $dbConn->query($query);
179+
if(!$result || $result->num_rows != 1) {
176180
return NULL;
177181
} else {
178-
$newId = mysql_fetch_assoc($result);
182+
$newId = $result->fetch_assoc();
179183
$newId = $newId['id'];
180184
$schedule = getScheduleFromId($newId);
181185
$schedule['id'] = $newId;
@@ -337,19 +341,19 @@ function renderSvg($svg, $id) {
337341
$query = "INSERT INTO schedules (oldid, startday, endday, starttime, endtime, building, quarter)" .
338342
" VALUES('', '{$json['startday']}', '{$json['endday']}', '{$json['starttime']}', '{$json['endtime']}', '{$json['building']}', " .
339343
" '{$json['term']}')";
340-
$result = mysql_query($query);
344+
$result = $dbConn->query($query);
341345
if(!$result) {
342-
die(json_encode(array("error" => "mysql", "msg" => "Failed to store the schedule: " . mysql_error($dbConn))));
346+
die(json_encode(array("error" => "mysql", "msg" => "Failed to store the schedule: " . $dbConn->error)));
343347
}
344348

345349
// Grab the latest id for the schedule
346-
$schedId = mysql_insert_id();
350+
$schedId = $dbConn->insert_id;
347351

348352
// Optionally process the svg for the schedule
349353
$image = false;
350354
if(!empty($_POST['svg']) && renderSvg($_POST['svg'], $schedId)) {
351355
$query = "UPDATE schedules SET image = ((1)) WHERE id = '{$schedId}'";
352-
mysql_query($query); // We don't particularly care if this fails
356+
$dbConn->query($query); // We don't particularly care if this fails
353357
}
354358

355359
// Now iterate through the schedule
@@ -360,18 +364,18 @@ function renderSvg($svg, $id) {
360364
foreach($item['times'] as $time) {
361365
$query = "INSERT INTO schedulenoncourses (title, day, start, end, schedule)" .
362366
" VALUES('{$item['title']}', '{$time['day']}', '{$time['start']}', '{$time['end']}', '{$schedId}')";
363-
$result = mysql_query($query);
367+
$result = $dbConn->query($query);
364368
if(!$result) {
365-
die(json_encode(array("error" => "mysql", "msg" => "Storing non-course item '{$item['title']}' failed: " . mysql_error($dbConn))));
369+
die(json_encode(array("error" => "mysql", "msg" => "Storing non-course item '{$item['title']}' failed: " . $dbConn->error)));
366370
}
367371
}
368372
} else {
369373
// Process each course. It's crazy simple now.
370374
$query = "INSERT INTO schedulecourses (schedule, section)" .
371375
" VALUES('{$schedId}', '{$item['id']}')";
372-
$result = mysql_query($query);
376+
$result = $dbConn->query($query);
373377
if(!$result) {
374-
die(json_encode(array("error" => "mysql", "msg" => "Storing a course '{$item['courseNum']}' failed: " . mysql_error($dbConn))));
378+
die(json_encode(array("error" => "mysql", "msg" => "Storing a course '{$item['courseNum']}' failed: " . $dbConn->erorr)));
375379
}
376380
}
377381
}

api/search.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,19 @@ function assertNumeric($var, $name) {
144144
}
145145

146146
// Run it!
147-
$result = mysql_query($query);
147+
$result = $dbConn->query($query);
148148
if(!$result) {
149149
echo json_encode(array("error" => "mysql", "msg" => "An error occurred while searching the database."));
150150
break;
151151
}
152-
if(mysql_num_rows($result) == 0) {
152+
if($result->num_rows == 0) {
153153
echo json_encode(array("error" => "result", "msg" => "No courses matched your criteria"));
154154
break;
155155
}
156156

157157
// Now we build an array of the results
158158
$courses = array();
159-
while($row = mysql_fetch_assoc($result)) {
159+
while($row = $result->fetch_assoc()) {
160160
$courses[] = $row['id'];
161161
}
162162
// @todo: store this in session to avoid lengthy and costly queries

api/status.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ function timeElapsed($time) {
4949
// MAIN EXECUTION //////////////////////////////////////////////////////////
5050
// Look up the last 20 scrape reports and store into an array
5151
$query = "SELECT * FROM scrapelog ORDER BY timeStarted DESC LIMIT 20";
52-
$result = mysql_query($query);
52+
$result = $dbConn->query($query);
5353
$lastLogs = array();
54-
while($row = mysql_fetch_assoc($result)) {
54+
while($row = $result->fetch_assoc()) {
5555
$lastLogs[] = $row;
5656
}
5757
echo json_encode($lastLogs);

0 commit comments

Comments
 (0)