简体   繁体   English

SQL查询返回错误的时间戳

[英]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 我正在研究一个涉及Sever-Sent事件的项目,并使用库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: PHP代码如下所示:

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". 但是,如果数据库中的值例如为“ 2015-12-16 11:00:00”,而当前时间为“ 2015-12-16 15:11:00”,则会得到以下输出:“ 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"). 如果我将三个查询放在单独的文件中并执行它们,则会得到所需的结果(“ 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". last_checked列的类型为timestamp,默认值为NULL,并且没有属性“ on update current timestamp”。

Does anyone know why I get the new timestamp twice when using the SSE? 有谁知道为什么在使用SSE时为什么要两次获得新的时间戳?

Because the timestamp format only goes down to seconds not milliseconds....and the update obviously doesn't take a whole second. 因为timestamp格式仅下降到秒而不是毫秒....并且更新显然不需要一秒钟。 Try putting a sleep(1) between the update and the query for update date to confirm this. 尝试在更新和更新日期查询之间放置sleep(1)来确认这一点。

     $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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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