猿问

如何对sql查询结果进行分页?

我有一些 PHP 代码,它从我的数据库中返回所有记录。每页我需要 10 个结果。如何对结果进行分页?不知道怎么写一段负责显示页码的代码...


<?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);

}

?>


12345678_0001
浏览 265回答 2
2回答

holdtom

以这种方式将 LIMIT 添加到您的查询中:SELECT *&nbsp;FROM news&nbsp;WHERE category='rhythmix'&nbsp;ORDER BY subject DESCLIMIT 0, 10;第一个数字 (0) 是结果集上的起始位置,第二个数字 (10) 是您要显示的结果数(偏移量)。显示第二页:...LIMIT 10, 10如果您总是想显示 10 个结果,您只需将 10 添加到 LIMIT 的第一个数字。

婷婷同学_

更改您的查询以阅读$query&nbsp;=&nbsp;$link->query("SELECT&nbsp;*&nbsp;FROM&nbsp;news&nbsp;WHERE&nbsp;category='rhythmix'&nbsp;ORDER&nbsp;BY&nbsp;subject&nbsp;DESC&nbsp;LIMIT&nbsp;0,&nbsp;10");显示前 10 行。下一组 10 行可以使用...LIMIT&nbsp;10,&nbsp;10");等等。.&nbsp;.
随时随地看视频慕课网APP
我要回答