-
Notifications
You must be signed in to change notification settings - Fork 0
/
getrecipe.php
68 lines (57 loc) · 1.19 KB
/
getrecipe.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
<?php
function connect_to_server(){
$mysqli = new mysqli(
"oniddb.cws.oregonstate.edu",
"cecilma-db","WWRm6SQCx2MUU1cl",
"cecilma-db"
);
if(!$mysqli || $msqli->connect_errno){
send_error($mysqli->connect_errno . ": " . $mysqli->connect_error);
}
return $mysqli;
}
function send_error($error){
$returnval = array(
"data" => false,
"success" => false
);
if ($error){
$returnval["error"] = $error;
}
echo json_encode($returnval);
exit();
}
$mysqli = connect_to_server();
if ($_GET){
$returnval = array(
"success" => true
);
if(array_key_exists("id", $_GET)){
$res = $mysqli->query("
SELECT flavor, ingredients
FROM ramen_recipes
WHERE (id = " . $_GET["id"] . ");
");
if($res){
$recipe = $res->fetch_object();
if ($recipe->flavor and $recipe->ingredients){
$returnval["data"] = array(
"flavor" => $recipe->flavor,
"ingredients" => $recipe->ingredients
);
}else{
send_error("Recipe not found.");
}
}else{
send_error("Recipe not found.");
}
echo json_encode($returnval);
}
else{
send_error("No recipe id given.");
}
}else{
send_error("No recipe id given, or id was given through POST.");
}
mysqli_close($mysqli);
?>