php - 从 MySQL 时间戳获取上次查看的项目

标签 php mysql

我正在尝试从 MySQL 数据库检索上次查看的页面,其结构如下:

文章表

  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++;
    } 
}

最佳答案

"SELECT * FROM `articles` ORDER BY `created` DESC LIMIT 1"

这应该获取最后查看的页面。

关于php - 从 MySQL 时间戳获取上次查看的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21057487/

相关文章:

MySql 获取最低行值,如果行存在则加 1

MySQL如何在没有外键的情况下级联删除

php - 将 Proxygen 或 NGINX + FastCGI 本地套接字与 HHVM 一起使用性能更好吗?

mysql - 根据时间戳自动更新字段

php - 没有显示数据 MySql php

php - 如何将搜索结果拆分为页面?

python - Django save() 方法不向我的 Mysql 数据库插入数据

mysql - SQL WHERE 子句标准

javascript - 如何使整个 <tr> 行在 laravel 中可点击?

php - 使用带有多个参数的 yii\rest\UrlRule 的 Yii2 路由