PHP last article and next article code

Source: Internet
Author: User
Tags mysql tutorial

Basic knowledge:

is to sort the ID pair with an ORDER by id DESC or an ordered by ID ASC, and then to judge the current id> or less than the current article ID
The SQL statement for the instance is as follows:

SELECT * FROM news where id< $id the ORDER by id DESC limit 0,1
SELECT * FROM news where id> $id the ORDER by id DESC limit 0,1

--
--The structure of the table ' String_find '
--

CREATE TABLE IF not EXISTS ' String_find ' (
' ID ' int (4) not NULL auto_increment,
' charList ' varchar 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, ' Deng Pioneer '),
(2, ' Deng Pioneer '),
(5, ' Fdafdsaf '),
(6, ' www.111cn.net ');

All right, let's see how it's done.
*/


MySQL tutorial _pconnect (' localhost ', ' root ', ' root ') or Die (Mysql_error ());
mysql_select_db (' cc ');
mysql_query ("Set names ' GBK '");
$cid =5;//is the number of your current article
$sql = "SELECT * from String_find where id> $cid the ORDER by id DESC limit 0,1"; Previous article
$sql 1 = "SELECT * from String_find where id< $cid the 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";
}

$result 1 = mysql_query ($sql 1);
if (mysql_num_rows ($result 1))
{
$rs 1 = mysql_fetch_array ($result 1);
echo "Next". $rs 1[0];
}
Else
{
echo "No";
}

This site original article reproduced annotated from Www.111cn.net

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.