-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquestionByKeyword.php
More file actions
58 lines (48 loc) · 1.19 KB
/
questionByKeyword.php
File metadata and controls
58 lines (48 loc) · 1.19 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
<?php
require_once __DIR__ . '/db_connect.php';
header("Content-Type: application/json; charset=utf-8");
$keyword_id = $_GET['keyword_id'] ?? '';
$search = $_GET['search'] ?? '';
if (!$keyword_id) {
echo json_encode([
"success" => false,
"error" => "keyword_id is required"
]);
exit;
}
// 🔹 기본 조건
$where = "WHERE keyword_id = ?";
$params = [$keyword_id];
$types = "i";
// 🔹 검색어 조건 추가
if ($search !== '') {
$where .= " AND (title LIKE ? OR body LIKE ?)";
$searchLike = "%{$search}%";
$params[] = $searchLike;
$params[] = $searchLike;
$types .= "ss";
}
$sql = "
SELECT question_id, author_id, body, keyword_id,
answer_count, title, like_count, view_count,
is_solved, created_at, updated_at
FROM questions
$where
ORDER BY question_id DESC
";
$stmt = $conn->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
$questions = [];
while ($row = $result->fetch_assoc()) {
$questions[] = $row;
}
echo json_encode([
"success" => true,
"sort" => "keyword",
"data" => $questions
], JSON_UNESCAPED_UNICODE);
$stmt->close();
$conn->close();
?>