Php previous and next article code

Source: Internet
Author: User

Code of the previous and next articles in the php tutorial
Basic knowledge:

Order by id desc or order by id asc is performed on the id pair, and then the result is judged to be smaller than the current id> or than the current Article id.
The SQL statement of the instance is as follows:

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

--
-- Table structure '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;

--
-- Export the data in the table 'string _ find'
--

Insert into 'string _ find '('id', 'charlist') VALUES
(1, 'Pioneer Deng '),
(2, 'Pioneer Deng '),
(5, 'fdafdsaf '),
(6, 'www. bKjia. c0m ');

Now everything is ready. Let's take a look at the operation method below.
*/


Mysql tutorial _ pconnect ('localhost', 'root', 'root') or die (mysql_error ());
Mysql_select_db ('cc ');
Mysql_query ("set names 'gbk '");
$ Cid = 5; // The number of your current article
$ SQL = "select * from string_find where id> $ cid order by id desc limit 0, 1"; // previous article
$ Sql1 = "select * from string_find where id <$ cid order by id asc limit 0, 1"; // next article
 
$ Result = mysql_query ($ SQL );
If (mysql_num_rows ($ result ))
{
$ Rs = mysql_fetch_array ($ result );
Echo "previous". $ rs [0];
}
Else
{
Echo "no ";
}
 
$ Result1 = mysql_query ($ sql1 );
If (mysql_num_rows ($ result1 ))
{
$ Rs1 = mysql_fetch_array ($ result1 );
Echo "Next". $ rs1 [0];
}
Else
{
Echo "no ";
}

The original article on this site is reprinted to indicate that it is from www. bKjia. c0m

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.