如何在向用户展示PHP MySQL之前验证通知数据



首先,我有一个常规通知表,该表有三个字段ID+正文+日期,从我的应用程序中,发送给用户的任何消息都会将此消息保存在此表中。现在的问题是,消息会发送给所有用户,所以我试图找到一种方法来找出哪些用户从其他用户那里看到了这条消息。现在我制作了另一个表来保存userID和notificationID。因此,如果IsWatchedNotifications表中的userID和NotificationsID,这意味着消息已经被查看,如果没有数据,那么这意味着到目前为止还没有查看消息。

现在我对这个查询有问题,我该怎么写才能这样工作?这是我现在的查询行:

$sql = "SELECT * FROM Notifications 
LEFT JOIN IsWatchedNotifications ON Notifications.NotificationsID = IsWatchedNotifications.NotificationsID
where  ";

完整代码:

<?php

$UserID= $_GET['UserID'];
$sql = "SELECT * FROM Notifications 
LEFT JOIN IsWatchedNotifications ON Notifications.NotificationsID = IsWatchedNotifications.NotificationsID
where  ";

$stmt = $con->prepare($sql); 
$stmt->bind_param("s",$UserID);
$stmt->execute();
$result = $stmt->get_result();

if ($result) {


while($row[] = $result->fetch_assoc()) {

$item = $row;

$json = json_encode($item, JSON_NUMERIC_CHECK);

}

} else {
}
echo $json;
$con->close();
?>

任何人都可以帮我谢谢

假设您的列名为user_id

您只需要添加带有占位符的条件

$sql = "SELECT * FROM Notifications 
LEFT JOIN IsWatchedNotifications ON Notifications.NotificationsID = IsWatchedNotifications.NotificationsID
where user_id = ?";

最新更新