I am creating message conversation script between two users in PHP MYSQLI.I want to know how to count unread message between two users.My script not showing anything.
my database pm table
id from_id to_id msg sent_date read
1 2 3 hi how are you? 2019-12-05 04:14:20 1
2 3 2 fine 2019-12-05 05:15:58 0
3 2 3 hi 2019-12-05 03:20:34 1
4 5 2 hi 2019-12-05 08:30:40 0
Here is my source code
<?php
$unread_messages = 0;
if (isset($_SESSION['userid'])) {
$session_id = $_SESSION['userid'];
}
$sql = ("SELECT COUNT(*) AS unread_messages
FROM pm
WHERE pm.to_id = ? and pm.from_id=from_id
AND read = 0");
if ($stmt = $con->prepare($sql)) {
$stmt->bind_param('i', $session_id);
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $unread_messages;
}
}
?>