从数据库返回以前的数据

h5qlskok  于 2021-06-24  发布在  Mysql
关注(0)|答案(0)|浏览(126)

我做了一个贴子评论应用,我只想显示2条评论,并允许用户点击“旧评论”显示更多的2条旧评论
问题是,每当我试图返回旧的注解而另一个用户刚刚被注解时,它就会返回无效的值

id       | comment       | time     | 
---------|---------------|----------|
1        | "hello"       | 10:30    |
2        | "test"        | 11:00    |
4        | "test"        | 12:00    |
6        | "test"        | 13:00    |

PHP代码

$startIndex = 0;

$qry = $db->prepare('SELECT --comment datas--
  ORDER BY time DESC LIMIT : startIndex, 2');
$qry->bindParam(':startIndex', $startIndex, PDO::PARAM_INT);
$qry->execute();

$comments = $qry->fetchAll(PDO::FETCH_ASSOC);
$comments = array_reverse($comments);

假设我已经加载了最后的评论(id 6,4)
然后有人加了一条评论。。

id       | comment       | time     | 
---------|---------------|----------|
1        | "hello"       | 10:30    |
2        | "test"        | 11:00    |
4        | "test"        | 12:00    |
6        | "test"        | 13:00    |
100      | "NEW"         | 20:00    |

现在,如果我想返回比(6,4),aka(2,1)旧的消息,我调用php文件

$startIndex = 2;

$qry = $db->prepare('SELECT --comment datas--
  ORDER BY time DESC LIMIT : startIndex, 2');
$qry->bindParam(':startIndex', $startIndex, PDO::PARAM_INT);
...

然后它返回(id 4,2):c
有没有一种方法可以坚持到最后加载的评论,并返回旧的评论?
如何定义$startindex?当用户第一次查看帖子时,评论的startindex为0。如果用户点击“以前的评论”,那么它将是2(0+2),我将它作为一个变量保存在应用程序中。当用户再次单击它时,它将是2+2,依此类推。。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题