I got some problems, when im trying to fetch the user with lastactivity time (-1 minute) i dont get any result at all.
我遇到了一些问题,当我试图用最后一次活动时间(-1分钟)获取用户时,我根本得不到任何结果。
This is my code:
这是我的代码:
if (isset($_POST['message'])) {
if (!empty($_POST['msg'])) {
$chatArray = array();
$s = $_database->query("INSERT INTO chat(`username`, `text`, `date`) VALUES('".$_database->real_escape_string($_POST['username'])."', '".$_database->real_escape_string($_POST['msg'])."', '".TIME()."')");
$newID = $_database->insert_id;
$_database->query("UPDATE chatusers SET lastactivity=TIME() WHERE username='".$_POST['username']."'");
$timestamp = (isset($_COOKIE['chatTimestamp'])) ? $_COOKIE['chatTimestamp'] : TIME();
$q = $_database->query("SELECT * FROM (SELECT * FROM chat WHERE `date` > ".$timestamp." ORDER BY date DESC LIMIT 0, 24) AS a ORDER BY DATE ASC");
if ($q->num_rows == 0) { echo '0'; }
else {
while ($rad = $q->fetch_array()) {
// $chatArray["message"][] = array("msgID" => $rad['msgID'], "message" => $rad['text'], "timestamp" => $rad['date'], "date" => date('H:i', $rad['date']), "username" => $rad['username']);
}
$query = $_database->query("SELECT * FROM chatusers WHERE `lastactivity` > (NOW() - INTERVAL 1 minute)");
var_dump($query);
while ($row = $query->fetch_array()) {
$chatArray["users"][] = array("username" => $row['name'], "lastactivity" => $row['lastactivity']);
}
print_r($chatArray);
}
}
}
As you can see i update the lastactivity before im trying to fetch the result, and i still get num_rows 0, if i change the select row to "SELECT * FROM chatusers WHERE lastactivity < (NOW() - 1 minute)"
i get all users in the database.
正如你所看到我在尝试获取结果之前更新lastactivity,我仍然得到num_rows 0,如果我将选择行更改为“SELECT * FROM chatusers WHERE lastactivity <(NOW() - 1分钟)”我得到所有数据库中的用户。
What am i missing here?
我在这里缺少什么?
1 个解决方案
#1
1
PHP's time()
, MySQL's TIME()
, and MySQL's NOW()
are not identical. Also, system time and the MySQL server time can be on different time zones.
PHP的time(),MySQL的TIME()和MySQL的NOW()不完全相同。此外,系统时间和MySQL服务器时间可以在不同的时区。
PHP's time()
returns a timestamp (an integer).
PHP的time()返回一个时间戳(一个整数)。
MySQL's TIME()
returns the time, as in 12:34:31
.
MySQL的TIME()返回时间,如12:34:31。
MySQL's NOW()
returns a date and time string, as in 2015-11-19 12:34:31
.
MySQL的NOW()返回日期和时间字符串,如2015-11-19 12:34:31。
Fix:
固定:
- if you are using a
datetime
column, use SQLNOW()
in all three statements (INSERT
,UPDATE
, andSELECT
). - 如果您使用的是datetime列,请在所有三个语句(INSERT,UPDATE和SELECT)中使用SQL NOW()。
- if you are using a
timestamp
column, use SQLTIME()
in all three. - 如果您使用的是时间戳列,请在所有三个中使用SQL TIME()。
#1
1
PHP's time()
, MySQL's TIME()
, and MySQL's NOW()
are not identical. Also, system time and the MySQL server time can be on different time zones.
PHP的time(),MySQL的TIME()和MySQL的NOW()不完全相同。此外,系统时间和MySQL服务器时间可以在不同的时区。
PHP's time()
returns a timestamp (an integer).
PHP的time()返回一个时间戳(一个整数)。
MySQL's TIME()
returns the time, as in 12:34:31
.
MySQL的TIME()返回时间,如12:34:31。
MySQL's NOW()
returns a date and time string, as in 2015-11-19 12:34:31
.
MySQL的NOW()返回日期和时间字符串,如2015-11-19 12:34:31。
Fix:
固定:
- if you are using a
datetime
column, use SQLNOW()
in all three statements (INSERT
,UPDATE
, andSELECT
). - 如果您使用的是datetime列,请在所有三个语句(INSERT,UPDATE和SELECT)中使用SQL NOW()。
- if you are using a
timestamp
column, use SQLTIME()
in all three. - 如果您使用的是时间戳列,请在所有三个中使用SQL TIME()。