title: AJAX SET INTERVAL
body: <p>I want to get the latest post_id in the table without refreshing it, but the problem is whenever a user inserts a value to the database, It echoes infinitely  the last post_id. I want it to echo only once. But I still want to get the latest post_id from the table.</p>
  
  <p>Here is my main php:</p>
  
  <pre><code>&lt;div id = "this_div"&gt;
          &lt;?php
              include 'connect.php';
              session_start();
              $query = "SELECT post_id FROM tbl_posts ORDER BY post_id ASC LIMIT 20";
              $execute_query = mysqli_query($con,$query);
              while($row = mysqli_fetch_assoc($execute_query))
              {
                  $get_this_id = $row['post_id'];
                  echo $get_this_id."&lt;br&gt;";
              }
              $_SESSION['get_this_id'] = $get_this_id;
          ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>here is my jQuery ajax:</p>
  
  <pre><code> &lt;script&gt;
              var refreshId = setInterval(function(){
              compare_session = "&lt;?php echo $_SESSION['get_this_id']; ?&gt;";
                  $.ajax({
                  url: 'another_file.php',
                  data: {},
                  success: function(data)
                  {
                      if(compare_session != data)
                      {
                          $('#this_div').text($('#this_div').text()+data);
                      }
                  }
                  });
              },400);
  &lt;/script&gt;
  </code></pre>
  
  <p>here is the php code of another_file.php</p>
  
  <pre><code>&lt;?php
    include 'connect.php';
    session_start();
    $query = "SELECT post_id FROM tbl_posts ORDER BY post_id DESC LIMIT 1";
    $execute_query = mysqli_query($con,$query);
    if($row = mysqli_fetch_assoc($execute_query))
    {
      echo $get_this_id = $row['post_id'];
    }
  ?&gt;
  </code></pre>