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