简体   繁体   中英

SQL query returns wrong timestamp

I'm working on a project which involves Sever-Sent Events and use the library libSSE: https://github.com/licson0729/libSSE-php

One event gets a timestamp from the database, does some work with it and then updates said timestamp value in the db. The PHP code looks like this:

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

require_once('lib/libsse/src/libsse.php');

//create the event handler
class messages extends SSEEvent {

    public function update($messages){
        //send notification data to the user
        return $messages;
    }

    public function check(){
          $sql = mysqli_connect("localhost", "username", "password");
          mysqli_select_db($sql, "db");

          $username = 'name';
          $a = mysqli_query($sql, "SELECT `last_checked` FROM `users` WHERE `name` = '".$username."';");
          $a = mysqli_fetch_row($a);

          mysqli_query($sql, "UPDATE `users` SET `last_checked` = now() WHERE `name` = '".$username."';");

          $b = mysqli_query($sql, "SELECT `last_checked` FROM `users` WHERE `name` = '".$username."';");
          $b = mysqli_fetch_row($b);

          return json_encode($a[0]."---".$b[0]);
    }
}

$sse = new SSE();//create a libSSE instance
//adjust libSSE settings
$sse->exec_limit = 0; //the execution time of the loop in seconds. Default: 600. Set to 0 to allow the script to run as long as possible.
$sse->sleep_time = 5; //The time to sleep after the data has been sent in seconds. Default: 0.5.
$sse->client_reconnect = 2; //the time for the client to reconnect after the connection has lost in seconds. Default: 1.
$sse->keep_alive_time = 10; //The interval of sending a signal to keep the connection alive. Default: 300 seconds.
$sse->addEventListener('messages',new messages());//register your event handler
$sse->start();//start the event loop

It should give the old and the new value to the update function which then sends the data to the user. However if the value in the db is for example "2015-12-16 11:00:00" and the current time is "2015-12-16 15:11:00" I get this output: "2015-12-16 15:11:00---2015-12-16 15:11:00".

So the new value two times instead of the old one first and then the new one.

If I put the three queries in a separate file and execute them I get the wanted result ("2015-12-16 11:00:00---2015-12-16 15:10:00"). The last_checked column has the type timestamp, the default value is NULL and it has not the attribute "on update current timestamp".

Does anyone know why I get the new timestamp twice when using the SSE?

Because the timestamp format only goes down to seconds not milliseconds....and the update obviously doesn't take a whole second. Try putting a sleep(1) between the update and the query for update date to confirm this.

     $a = mysqli_query($sql, "SELECT `last_checked` FROM `users` WHERE `name` = '".$username."';");
      $a = mysqli_fetch_row($a);

      mysqli_query($sql, "UPDATE `users` SET `last_checked` = now() WHERE `name` = '".$username."';");

sleep(1); //sleep for 1 second to prove my point

      $b = mysqli_query($sql, "SELECT `last_checked` FROM `users` WHERE `name` = '".$username."';");
      $b = mysqli_fetch_row($b);

      return json_encode($a[0]."---".$b[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM