mysql_qury及pdo方式制作上一篇和下一篇文章

内容展示型网页中经常会使用到上一篇和下一篇文章的链接,不但方便读者查找内容分集,也是网站内链优化的一种方式。

这篇笔记以mysql_qury及pdo方式,记录一下CMS中上下篇的制作的一种原始方式。
$id = isset($_get['id']) > 0 ? intval($_get['id']) : "";//获取get传参id值
一、mysql_fetch_array方式
下一篇:
$query = mysql_query("select id,title from article where id>'$id' order by id asc limit 1");
$next = mysql_fetch_array($query);
上一篇:
$query = mysql_query("select id,title from article where id <'$id' order by id desc limit 1");
$prev = mysql_fetch_array($query);
或者:
{$id} 代表当前id值。
select * from table_a where id = (select id from table_a where id < {$id} order by id desc limit 1);
select * from table_a where id = (select id from table_a where id > {$id} order by id asc limit 1);
或者:
{$id} 代表当前id值。
select * from table_a where id = (select max(id) from table_a where id < {$id});
select * from table_a where id = (select min(id) from table_a where id > {$id});
二、pdo的fetch方式
上一篇:
$sql="select id,title from article where id <'$id' order by id desc limit 1"; $stmt=$pdo->prepare($sql);
$stmt->execute();
$prev = $stmt->fetch();
获取下一篇同理。

三、输出上下篇文章
pdo的展示方式可根据需要整理,上下篇文章制作完成。

#现在前往

精选留言

一篇,mysql,qury,pdo
sample
2022-11-19
写留言
签到
投稿
QQ咨询
返回顶部