-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.php
More file actions
70 lines (64 loc) · 2.07 KB
/
common.php
File metadata and controls
70 lines (64 loc) · 2.07 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
67
68
69
70
<?php
# Common functions for the lyric/phrase API.
# returns: a new database connection
function get_db() {
$db = new PDO("mysql:host=wbigelow.vergil.u.washington.edu;port=12546;dbname=codeday;charset=utf8", "root", "root");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
}
# Inserts a phrase into the given database.
# params:
# $db: a database connection
# $phrase: a phrase to be inserted
function insert($db, $phrase) {
$insert = "INSERT INTO Phrases (phrase) ";
$values = "VALUES ('$phrase');";
$db->exec($insert . $values);
}
# Removes a phrase from the given database.
# params:
# $db: a database connection
# $phrase: a phrase to be removed
function delete($db, $phrase) {
$db->exec("DELETE FROM Phrases WHERE phrase = '$phrase';");
}
# Checks to see if a phrase is in the given database.
# params:
# $db: a database connection
# $phrase: a phrase to be checked
# returns:
# an error if the phrase is in the database
function contains($db, $phrase) {
$count = count($db->query("SELECT * FROM Phrases WHERE phrase = '$phrase';")->fetchAll());
if($count) {
$output["error"] = "Error: Phrase already in database.";
return $output;
}
}
# Gets all phrases containing the word
# params:
# $db: a database connection
# $phrase: a phrase to be checked
# returns:
# an error if the phrase is in the database
function findall($db, $word) {
return $db->query("SELECT * FROM Phrases WHERE phrase LIKE '%$word%';")->fetchAll();
}
# Creates an error response for when a request is made with invalid parameters
# params:
# $params: an array of length 1 or 2 of the missing parameters
# $conjoiner: "and" or "or" for when there are two parameters
# returns:
# An error respsonse JSON object
function missing_param($params, $conjoiner = null) {
header("HTTP/1.1 400 Invalid Request");
$paramOne = $params[0];
$error = "Missing $paramOne";
if($params[1]) {
$error = $error . " " . $conjoiner . " " . $params[1];
}
$error = $error . " parameter";
$output["error"] = $error;
return $output;
}
?>