-
Notifications
You must be signed in to change notification settings - Fork 2
/
getPosts.php
40 lines (30 loc) · 942 Bytes
/
getPosts.php
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
<?php
$servername = "127.0.0.1";
$username = "xbnaulyx_1";
$password = "Zb100tdwtok@";
$dbname = "xbnaulyx_blogs";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM blog_posts ORDER BY date_created DESC";
$result = $conn->query($sql);
$posts = array();
while($row = $result->fetch_assoc()) {
$postId = $row["id"];
// Fetch comments for this post
$commentsSql = "SELECT * FROM comments WHERE post_id = $postId ORDER BY date_created DESC";
$commentsResult = $conn->query($commentsSql);
$comments = array();
while($commentRow = $commentsResult->fetch_assoc()) {
$comments[] = $commentRow;
}
// Add comments to the post
$row["comments"] = $comments;
$posts[] = $row;
}
echo json_encode($posts);
$conn->close();
?>