简体   繁体   中英

How to paginate sql query result?

I have some PHP code, which returns all records from my database. I need 10 results on each page. How to paginate the results? Dunno how to write a piece of code responsible for displaying page numbers...

<?php
$query = $link->query("SELECT * FROM news WHERE category='rhythmix' ORDER BY subject DESC"); 

while($output = $query->fetch_assoc()){
$text = $output['news'];
$text = str_replace('[video]','<div class="video-container">',$text);
$text = str_replace('[/video]','</div>',$text);
$text = str_replace('[media]','<center>',$text);
$text = str_replace('[/media]','</center>',$text);
$embera = new \Embera\Embera();
echo $embera->autoEmbed($text);
}
?>

Change your query to read

$query = $link->query("SELECT * FROM news WHERE category='rhythmix' ORDER BY subject DESC LIMIT 0, 10");

To display the first 10 rows. The next set of 10 rows can be shown using

...LIMIT 10, 10");

and so on. . .

Add LIMIT to your query in this way:

SELECT * 
FROM news 
WHERE category='rhythmix' 
ORDER BY subject DESC
LIMIT 0, 10;

The first number (0) is the start position on the resulset and the second one (10) is how many results you want to show (the offset).

To show the second page:

...LIMIT 10, 10

If you always want to show 10 results you just need to to add 10 to the first number of the LIMIT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM