1 回答

TA贡献1815条经验 获得超6个赞
使用 LEFT JOIN 到love表格并检查帖子是否真的被喜欢/喜欢的IS NOT NULL条件:
<?php
// Get records from the database
$stmt = $db->prepare("
SELECT p.*, (l.postid IS NOT NULL) as is_liked
FROM posts p
LEFT JOIN love l
ON l.postid = p.id
AND l.userid = ?
ORDER BY p.id DESC
LIMIT 10
");
$stmt->bind_param('i', $currentUserId);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$postID = $row['id'];
?>
[...]
您可以访问$row['is_liked']将包含1或的值0。
您需要替换$currentUserId为包含当前使用 ID 的变量。那可能是$_SESSION['userid']。
- 1 回答
- 0 关注
- 116 浏览
添加回答
举报