title: How to receive any changes in MySQL database in real time from client code in a timed function?
body: <p>I have a database with a log record table which looks like this:</p>
  
  <pre><code>+-----------+------------------------+--------+
  | Timestamp | Symbol_name            | Status |
  +-----------+------------------------+--------+
  |         1 | Group2                 |      1 |
  |         2 | Group1-Device3-Signal1 |      1 |
  |         3 | Group2-Device1-Signa13 |      0 |
  +-----------+------------------------+--------+
  </code></pre>
  
  <p>Where <code>Timestamp</code> is a <code>double</code>, <code>Symbol_name</code> is a <code>varchar</code>, and <code>Status</code> is an <code>int</code>.</p>
  
  <p>Log records which contain the above data are going to be inserted into the table in real time, and my client code is supposed to query those records and analyze them. The problem I am having right now is reading a unique record each query. Currently, I have this function:</p>
  
  <pre><code>/* Called every 1000 ms (1 second). */
  gboolean app_models_timed_query(gpointer data) {
      FwApp *app = data;
  
      char query[APP_MAXIMUM_STRING_CHARS];
  
      strncpy(query, "SELECT * FROM ", APP_MAXIMUM_STRING_CHARS);
      strncat(query, app-&gt;query_db_table_name, APP_MAXIMUM_STRING_CHARS);
      strncat(query, " WHERE Timestamp &lt;&gt; @lastSeenTimestamp AND Symbol_name &lt;&gt; @lastSeenSymbolName AND Status &lt;&gt; @lastSeenStatus;", APP_MAXIMUM_STRING_CHARS);
  
      if (mysql_query(app-&gt;query_db_con, query))
      {
          printf("Unable to retrieve data from query table.\n");
          return TRUE;
      }
  
      MYSQL_RES *result = mysql_store_result(app-&gt;query_db_con);
  
      if (result == NULL) return TRUE;
  
      /* Analyze the resulting row(s) here. */
  
      /* How to set @lastSeenTimestamp, @lastSeenSymbolName and @lastSeenStatus here? */
  
      return TRUE;
  }
  </code></pre>
  
  <p>The function gets called every second, and in it, I query the database using the following statement:</p>
  
  <pre><code>SELECT * FROM table1 WHERE Timestamp &lt;&gt; @lastSeenTimestamp AND Symbol_name &lt;&gt; @lastSeenSymbolName AND Status &lt;&gt; @lastSeenStatus;
  </code></pre>
  
  <p>No two records will ever be exactly the same, but they can have the same timestamp, status, or symbol name.</p>
  
  <p>Note that before I enable <code>app_models_timed_query</code> to be called each second, I set the user-defined variables like so:</p>
  
  <pre><code>SET @lastSeenTimestamp = -1, @lastSeenSymbolName = '', @lastSeenStatus = 0;
  </code></pre>
  
  <p>And since timestamps will never be negative, the first time <code>app_models_timed_query</code> is called, the first row will be in the result of the query.</p>
  
  <p>However, my question is how to set the user-defined variables to the last row of the result of my query. Also, I want to know if there is a better way of reading only newly inserted rows each time <code>app_models_timed_query</code> is called.</p>
  
  <p>Many thanks,
  Vikas</p>