-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_user_conversation.php
More file actions
56 lines (45 loc) · 1.48 KB
/
get_user_conversation.php
File metadata and controls
56 lines (45 loc) · 1.48 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
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lost_found_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$response = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$user_id = $_POST['user_id'];
// Fetch the conversation history for the user
$sql = "SELECT sender_id, receiver_id, message, created_at FROM user_messages
WHERE sender_id = ? OR receiver_id = ? ORDER BY created_at ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $user_id, $user_id);
if ($stmt->execute()) {
$result = $stmt->get_result();
$conversations = array();
while ($row = $result->fetch_assoc()) {
$conversation = array(
"sender_id" => $row['sender_id'],
"receiver_id" => $row['receiver_id'],
"message" => $row['message'],
"created_at" => $row['created_at']
);
$conversations[] = $conversation;
}
$response['success'] = true;
$response['conversations'] = $conversations;
} else {
$response['success'] = false;
$response['message'] = "Failed to load conversations.";
}
$stmt->close();
} else {
$response['success'] = false;
$response['message'] = "Invalid request.";
}
$conn->close();
echo json_encode($response);
?>