This article mainly introduces how PHP implements the next article in the previous article, and summarizes and analyzes the query skills related to php obtaining SQL operations in the previous article in the form of examples, for more information about how to implement PHP in the previous article, see the example in this article. We will share this with you for your reference. The details are as follows:
Php implements the next article in the previous article. This article mainly uses SQL to judge based on the current id and then filter out the data before or after the current ID, let's take a look at the specifics.
Implements the writing of SQL statements in the previous and next articles on the website.
The id of the current article is $ article_id, and the id of the corresponding category of the current article is $ cat_id, so the previous article should be:
SELECT max(article_id) FROM article WHERE article_id < $article_id AND cat_id=$cat_id;
Run this SQL statement and get $ max_id. then
SELECT article_id, title FROM article WHERE article_id = $max_id;
To simplify the process, convert it to subquery:
SELECT article_id, title FROM article WHERE article_id = (SELECT max(article_id) FROM article WHERE article_id < $article_id AND cat_id=$cat_id);
The code below is as follows:
SELECT min(article_id) FROM article WHERE article_id > $article_id AND cat_id=$cat_id;
Run this SQL statement and get $ min_id. Then:
SELECT article_id, title FROM article WHERE article_id = $min_id;
To simplify the process, convert it to subquery:
SELECT article_id, title FROM article WHERE article_id = (SELECT min(article_id) FROM article WHERE article_id > $article_id AND cat_id=$cat_id);
Finally, many of my friends prefer to use the following statements:
In the previous article, the code is as follows:
select id from table where id10 limit 0,1;
This is certainly no problem, but the performance is not very good.
SQL statement optimization:
You can use union all to obtain three rows of data in a statement, provided that the three query fields are the same. the first row of the query result is the previous article, the second line is the current article, and the third line is the next article. the code is as follows:
(select id from table where id < 10 order by id asc limit 1) union all (select id from table where id = 10) union all (select id from table where id > 10 order by id desc limit 1);
Now let's take a look at some examples in cms to implement phpcms in the previous article.
Obtain the id of the current browser article:
$id = isset($_GET['id']) > 0 ? intval($_GET['id']) : "";
Next Article:
$query = mysql_query("SELECT id,title FROM article WHERE id>'$id' ORDER BY id ASC LIMIT 1");$next = mysql_fetch_array($query);
Previous article:
$query = mysql_query("SELECT id,title FROM article WHERE id <'$id' ORDER BY id DESC LIMIT 1");$prev = mysql_fetch_array($query);
I hope this article will help you with PHP programming.
For more information about PHP implementation in the previous article, refer to PHP Chinese network!