-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgetQuestion.php
More file actions
executable file
·57 lines (43 loc) · 1.37 KB
/
getQuestion.php
File metadata and controls
executable file
·57 lines (43 loc) · 1.37 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
<?php
//Get an existing question based on the id and return question info or an error
//Paul Geromini
//Get the data from the app
$id = $_POST["ID"];
//Connect to the db
mysql_connect("localhost","root","root") or die("Unable to connect");
mysql_select_db("questiondb");
//Retrieve question based on id
$question = mysql_query("SELECT * FROM question WHERE id='$id'");
//Check if we got a result
if(mysql_numrows($question) == null){
//If so respond with error
$Array = array("ERROR" => "Question with id: '$id', does not exist" );
echo json_encode($Array);
//Close our db connection
mysql_close();
return;
}
//Retrieve all selected answers
$ans = mysql_query("SELECT * FROM responses WHERE questionid = '$id'");
//Close our db connection
mysql_close();
//Retrieve the row
$row = mysql_fetch_array($question);
//Store data into each var
$title = $row['Title'];
$questionContent = $row['Question'];
$possibleResponses = $row['Answers'];
//Check to see if there are any responses
if(mysql_numrows($ans) == null){
//if not
$answers = "NONE";
} else {
while ($row = mysql_fetch_array($ans)){
$answers[] = $row['answer'];
}
}
//Respond with question info
$Array = array("ERROR" => "NONE", "Title" => $title, "Question" => $questionContent,
"PossibleResponse" => $possibleResponses, "Responses" => $answers);
echo json_encode($Array);
?>