我正在尝试从MySQL数据库中检索最后查看的页面,它的结构如下:
articles table
id | title | content | created
----------------------------------------
1 | title-1 | info-1 | 1386478352
2 | title-2 | info-2 | 1386532855
3 | title-3 | info-3 | 1386637325
4 | title-4 | info-4 | 1386812249
5 | title-5 | info-5 | 1387094028 我想获得最后查看的页面,不确定多少时间是足够的,因为每次网站上的页面刷新这个函数都会被调用。但是,这就是我尝试过的,但是它没有返回任何东西。
"SELECT * FROM articles WHERE TIMESTAMPDIFF(MINUTE, created,NOW()) < 10 LIMIT 2"但正如我所说的,它不会返回任何数据。这就是我试图打印信息的方式。
$loopOne = 0;
foreach($articleLastView as $recent)
{
$recent_id = $recent['id'];
$recent_title = $recent['title'];
$recent_created = $recent['created'];
if ( $loopOne == 0 )
{
echo 'I print info here if result is the first';
$loopOne++;
}
else if( $loopOne == 1 )
{
echo 'I print info here if result is the second';
$loopOne++;
}
}发布于 2014-01-11 09:47:57
"SELECT * FROM `articles` ORDER BY `created` DESC LIMIT 1"这应该会得到最后一次查看的页面。
发布于 2014-01-11 10:29:39
我认为您的函数不工作的原因是您传入了一个UNIX时间戳,而该函数需要两个datetime表达式。
根据Doc的说法
TIMESTAMPDIFF(单位,datetime_expr1,datetime_expr2)
返回datetime_expr2 - datetime_expr1,其中datetime_expr1和datetime_expr2是日期或日期时间表达式。
第一个表达式是UNIX时间戳,而NOW()表达式是日期时间。
尝试将UNIX时间戳转换为日期时间格式。
例如:
SELECT *
FROM articles
WHERE TIMESTAMPDIFF(MINUTE, FROM_UNIXTIME(created), NOW()) < 10
LIMIT 2https://stackoverflow.com/questions/21057487
复制相似问题