Skip to content

Commit 38b97af

Browse files
authored
Update fetch_question.php
1 parent 0fe1d66 commit 38b97af

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

fetch_question.php

+37
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
1+
<?php
2+
$servername = "localhost";
3+
$username = "root";
4+
$password = "";
5+
$dbname = "question_db";
6+
7+
$conn = new mysqli($servername, $username, $password, $dbname);
8+
9+
if ($conn->connect_error) {
10+
die("Connection failed: " . $conn->connect_error);
11+
}
12+
13+
$question_id = 1; // You can change this to fetch different questions
14+
15+
$question_sql = "SELECT * FROM questions WHERE id = $question_id";
16+
$question_result = $conn->query($question_sql);
17+
18+
$options_sql = "SELECT * FROM options WHERE question_id = $question_id";
19+
$options_result = $conn->query($options_sql);
20+
21+
$response = array();
22+
23+
if ($question_result->num_rows > 0) {
24+
$response['question'] = $question_result->fetch_assoc();
25+
}
26+
27+
if ($options_result->num_rows > 0) {
28+
$response['options'] = array();
29+
while($row = $options_result->fetch_assoc()) {
30+
$response['options'][] = $row;
31+
}
32+
}
33+
34+
echo json_encode($response);
35+
36+
$conn->close();
37+
?>
138

0 commit comments

Comments
 (0)