forked from actuallyaridan/chirp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_chirps.php
33 lines (28 loc) · 1.09 KB
/
fetch_chirps.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
<?php
try {
$db = new PDO('sqlite:' . __DIR__ . '/chirp.db');
$offset = isset($_GET['offset']) ? (int)$_GET['offset'] : 0;
$limit = 6;
$query = 'SELECT chirps.*, users.username, users.name, users.profilePic, users.isVerified
FROM chirps
INNER JOIN users ON chirps.user = users.id
ORDER BY chirps.timestamp DESC
LIMIT :limit OFFSET :offset';
$stmt = $db->prepare($query);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$chirps = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$row['chirp'] = nl2br(htmlspecialchars($row['chirp']));
$row['username'] = htmlspecialchars($row['username']);
$row['name'] = htmlspecialchars($row['name']);
$row['profilePic'] = htmlspecialchars($row['profilePic']);
$row['isVerified'] = (bool)$row['isVerified'];
$chirps[] = $row;
}
echo json_encode($chirps);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>