forked from FindMEmory/FindMEmory_BE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_keyword.php
More file actions
56 lines (46 loc) · 919 Bytes
/
search_keyword.php
File metadata and controls
56 lines (46 loc) · 919 Bytes
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
<?php
require_once __DIR__ . '/db_connect.php';
header('Content-Type: application/json; charset=utf-8');
$query = trim($_GET['query'] ?? '');
if ($query === '') {
echo json_encode([
"success" => true,
"keywords" => []
]);
exit;
}
$sql = "
SELECT
keyword_id AS id,
name,
question_count,
participant_count,
created_at
FROM keywords
WHERE name LIKE ?
ORDER BY question_count DESC, created_at DESC
LIMIT 20
";
$stmt = $conn->prepare($sql);
if (!$stmt) {
echo json_encode([
"success" => false,
"error" => "SQL prepare failed"
]);
$conn->close();
exit;
}
$like = "%{$query}%";
$stmt->bind_param("s", $like);
$stmt->execute();
$result = $stmt->get_result();
$keywords = [];
while ($row = $result->fetch_assoc()) {
$keywords[] = $row;
}
$stmt->close();
$conn->close();
echo json_encode([
"success" => true,
"keywords" => $keywords
]);