php 上一篇與下一篇文章代碼
來源:程序員人生 發(fā)布時間:2013-11-16 05:39:07 閱讀次數(shù):2778次
基本知識:就是對id對進(jìn)行order by id desc 或 order by id asc進(jìn)行排序,然后再判斷比當(dāng)前id > or小于當(dāng)前文章id的,實(shí)例的sql語句如下:
select * from news where id<$id order by id desc limit 0,1
select * from news where id>$id order by id desc limit 0,1
表的結(jié)構(gòu) `string_find`
- CREATE TABLE IF NOT EXISTS `string_find` (
- `id` int(4) NOT NULL auto_increment,
- `charList` varchar(100) default NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
導(dǎo)出表中的數(shù)據(jù) `string_find`
- INSERT INTO `string_find` (`id`, `charList`) VALUES
- (1, '鄧先驅(qū)者'),
- (2, '鄧先驅(qū)者'),
- (5, 'fdafdsaf'),
- (6, 'www.phpfensi.com');
好了萬事俱備了,下面來看一下操作方法:
- */
- mysql教程_pconnect('localhost','root','root') or die(mysql_error());
- mysql_select_db('cc');
- mysql_query("set names 'gbk'");
- $cid =5;
- $sql ="select * from string_find where id>$cid order by id desc limit 0,1";
- $sql1 ="select * from string_find where id<$cid order by id asc limit 0,1";
-
- $result = mysql_query( $sql );
- if( mysql_num_rows( $result ) )
- {
- $rs = mysql_fetch_array( $result );
- echo "上一篇".$rs[0];
- }
- else
- {
- echo "沒有了";
- }
-
- $result1 = mysql_query( $sql1 );
- if( mysql_num_rows( $result1 ) )
- {
- $rs1 = mysql_fetch_array( $result1 );
- echo "下一篇".$rs1[0];
- }
- else
- {
- echo "沒有了";
- }
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈