title: Check for new entries in a DB
body: <p>I am trying to build an application that will query a db, and send data somewhere as it comes into the database. </p>
  
  <p>I can get the data I want from the database using this code:</p>
  
  <pre><code>$sql="SELECT * FROM `signals` order by `time` DESC LIMIT 100";
  $result = mysqli_query($DatabasePointer,$sql) 
      or die(mysqli_error($DatabasePointer));
  $row_cnt = mysqli_num_rows($result);
  
  if($row_cnt&gt;0)
  {
      $array = array();
      $i=0;
  
      while($row = mysqli_fetch_array($result))
      {
          $i++;
          //$a = array();
          $epoch = $row['time'];
          // convert UNIX timestamp to PHP DateTime
          $dt = new DateTime("@$epoch");
  
          if(
              ($row['symbol'] === 'USDJPYecn') 
              || ($row['symbol'] === 'USDCADecn') 
              || ($row['symbol'] === 'EURUSDecn')
              ) 
          {
              if(
                  ($row['timeframe'] === 'M5') 
                  || ($row['timeframe'] === 'M15')
                  )  
              {
                  $a = array(
                      "time" =&gt; $dt-&gt;format('Y-m-d H:i:s'),
                      "signal" =&gt; $row['signal'],
                      "symbol" =&gt; $row['symbol'],
                      "price" =&gt; $row['price'],
                      "timeframe" =&gt; $row['timeframe'],
                      "epoch" =&gt; $row['time'],
                      "candel" =&gt; $row['candel'] 
                      );
                  $array[] = $a;
               }
          }
  
      } // while
  
      echo json_encode($array, JSON_UNESCAPED_SLASHES);           
  }
  </code></pre>
  
  <p>However, I am not sure how to revise the code to check to see if the data is new, or has already been sent out to another source. I am also unsure how to revise the code to only send new data as it hits the db, not an entire array of data like I am calling now. </p>
  
  <p>Can somebody please point me in the right direction?</p>
  
  <p>EDIT:</p>
  
  <pre><code>$sql="SELECT * FROM `tdisignals` order by `time` DESC LIMIT 100";
  $result = mysqli_query($DatabasePointer,$sql) 
      or die(mysqli_error($DatabasePointer));
  $row_cnt = mysqli_num_rows($result);
  
  if($row_cnt&gt;0)
  {
      $array = array();
      $i=0;
      while($row = mysqli_fetch_array($result))
      {
          $i++;
          //$a = array();
          $epoch = $row['time']; 
          // convert UNIX timestamp to PHP DateTime
          $dt = new DateTime("@$epoch");
  
          if( 
              $row['symbol'] === 'USDJPYecn' 
              || ($row['symbol'] === 'USDCADecn') 
              || ($row['symbol'] === 'GBPUSDecn'))
          {
              if(
                  $row['timeframe'] === 'M5')
                  || ($row['timeframe'] === 'M15'))
              {
  
                  $a = array(
                      "time" =&gt; $dt-&gt;format('Y-m-d H:i:s'),
                      "signal" =&gt; $row['signal'],
                      "symbol" =&gt; $row['symbol'],
                      "price" =&gt; $row['price'],
                      "timeframe" =&gt; $row['timeframe'],
                      "epoch" =&gt; $row['time'],
                      "candel" =&gt; $row['candel'],
                      );
  
                  $array[] = $a;
              }
  
          }
  
      }
  
      // echo json_encode($array, JSON_UNESCAPED_SLASHES);
      $fuegostore = json_encode($array, JSON_UNESCAPED_SLASHES);
      // $sql2 = "INSERT INTO fuegosync (time, lastsync) ".
      //          "VALUES ('$date', '$fuegostore')";
      // $result2 = mysqli_query($DatabasePointer,$sql2) 
         // or die(mysqli_error($DatabasePointer));
      $sql3="SELECT lastsync, MAX(CAST(time AS CHAR)) FROM `fuegosync`";
      $result3 = mysqli_query($DatabasePointer,$sql3) 
          or die(mysqli_error($DatabasePointer));
  
      $row2 = mysqli_fetch_row($result3);
  
      if($row2[0] === $fuegostore)
          echo 'No New Signals';
      else
          echo 'New Signals';
                      ///OPTION 1:
                      //print_r (json_encode($array[0], JSON_UNESCAPED_SLASHES));
                      //OPTION 2:
                      foreach($array as $x) {
                          if(strtotime($array[0]['time']) &gt;= $row2[1]) {
                          echo '&lt;br /&gt;&lt;span&gt;'.$x['signal'].' - '.$x['symbol'].' - '.$x['price'].'&lt;br /&gt;';
                          } else {
                              echo 'No New Signals';
                          }
                  }
  
      echo $row2[0];
  }
  </code></pre>
  
  <p>This code is successfully detecting a new data hitting the database. What I am struggling with now, is revising the code to only display the newly detected data piece, not the entire array as you see it. </p>
  
  <p>NEW EDIT:
  I have gotten the code to display only the newest piece of data, but now I have a conundrum.
  If I poll the database say every minute, and a new piece of data hits the db, the script will pick it up -- however if another new piece of data hits the db seconds after the first new piece was sent to target, the second new piece will totally get ignored because the poll would be every minute. I would basically have to poll the database every few seconds... and that sounds like a performance nightmare... </p>
  
  <p>The grayed out OPTION 1  is what displays the newest data, but would skip a newer piece before a minute based poll unless the db was polled every second.
  OPTION 2 works, but displays the entire array... so I am unsure how to revise the code to only display the newest pieces, not the entire thing.</p>
  
  <p>Any suggestions?</p>