簡單的PHP+Mysql查詢上一篇和下一篇文章實例,並輸出上一篇和下一篇文章的標題和連接,適合新手學習php
獲取當前瀏覽文章id:mysql
$id = isset($_GET['id']) > 0 ? intval($_GET['id']) : "";
下一篇文章:sql
$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);
輸出:fetch
<div class="page_nav clearfix"> <div title="下一篇" class="next_page"> <a href="?id=<?php echo $next['id']; ?>"><?php echo $next['title']; ?></a> </div> <div title="上一篇" class="pre_page"> <a href="?id=<?php echo $prev['id']; ?>"><?php echo $prev['title']; ?></a> </div> </div>