0

Purpose: Chat system - it updates the page automatically to find new messages

Explanation of the code: 1. Last message that existed when the page is accessed for the first time. 2. Find new messages (they have grater id-s)

Problem: The first works ok, but the second part (the function) does't work

<?php
    include('config.php');
    $query = "SELECT MAX(id) FROM comments";
    $res = mysqli_query($connect,$query); 
    $rez = mysqli_fetch_assoc($res);
    $id = $rez['MAX(id)'];

    $query1 = "SELECT com FROM comments";
    $res1 = mysqli_query($connect,$query1); 
?>
<!DOCTYPE html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<style>
.verification{
  overflow-y:scroll;
  height:300px;
  width:100px;
}
</style>

<script>
setInterval(update, 1000);

function update() {
    <?php
    $query = "SELECT MAX(id) FROM comments";
    $res = mysqli_query($connect,$query);
    $row = mysqli_fetch_assoc($res);
    if($row['MAX(id)'] > $id)
    {  
         $query1 = "SELECT com FROM comments";
         $res1 = mysqli_query($connect,$query1);
         while($row = mysqli_fetch_assoc($res1))
         { ?>
             $('#verification').html("(<b>" + <?php echo $row['com']; ?> + "</b>)");
  <?php  }   ?>
<?php  }  ?>
}


</script>

</head>

<body>
<div id="verification">
<?php
while($row = mysqli_fetch_assoc($res1))
         { 
             echo '<div>'.$row['com'].'</div>';
         }   ?>
</div>

</body>

</html>
Popescu Ion
  • 142
  • 10

0 Answers0