如何将查询结果划分为页面?

时间:2022-05-31 15:45:05

I want to implement a "page" system so that in the first page I show 10 results (1-10), in the 2nd page I show the other 10 (11-20) results. etc.

我想实现一个“页面”系统,以便在第一页中我显示10个结果(1-10),在第二页中我显示其他10个(11-20)结果。等等

The results should be ordered in different ways, for example ORDER BY count DESC; Is there a pure MySQL solution for this, like

结果应以不同方式排序,例如ORDER BY count DESC;是否有一个纯粹的MySQL解决方案,如

$results_per_page = 10;
$page_number = $_GET['page'];

mysql_query( "SELECT * FROM mytable ORDER BY count DESC 
LIMIT $results_per_page*$page , $results_per_page*($page+1) // Of course, I made up this line
;");

3 个解决方案

#1


0  

I found the answer to my question, I should select them like

我找到了问题的答案,我应该选择它们

SELECT * FROM mytable ORDER BY whatever DESC LIMIT 10 OFFSET 10;

for the 2nd page.

为第2页。

so with the variables it becomes

所以变量就变成了

SELECT * FROM mytable ORDER BY whatever DESC LIMIT $results_per_page OFFSET $page_number-1;

#2


0  

After cleaning the $page variable, since you should never accept input straight from the user for security reasons, you should do this:

在清理$ page变量之后,由于出于安全原因,您不应该直接接受来自用户的输入,因此您应该这样做:

<?php
$first = $results_per_page * ($page-1); // on page 1 you want to start at row 0
$querystring = "SELECT * FROM mytable ORDER BY count DESC 
LIMIT $first, $results_per_page";
?>

#3


0  

make a function passing four parameters
function getresult($index,$order,$start,$no_rows)
 {

    $sql="SELECT * FROM mytable order by $index $orders limit $start,$no_rows";
 }

where,

$index=on which sorting u want to do

$ index =你要做的排序

$order=asc/desc

$start=from on yow will start to get records

$ start = from on yow将开始获取记录

$no_$rows=you want to get no of rows

$ no_ $ rows =你想要没有行

#1


0  

I found the answer to my question, I should select them like

我找到了问题的答案,我应该选择它们

SELECT * FROM mytable ORDER BY whatever DESC LIMIT 10 OFFSET 10;

for the 2nd page.

为第2页。

so with the variables it becomes

所以变量就变成了

SELECT * FROM mytable ORDER BY whatever DESC LIMIT $results_per_page OFFSET $page_number-1;

#2


0  

After cleaning the $page variable, since you should never accept input straight from the user for security reasons, you should do this:

在清理$ page变量之后,由于出于安全原因,您不应该直接接受来自用户的输入,因此您应该这样做:

<?php
$first = $results_per_page * ($page-1); // on page 1 you want to start at row 0
$querystring = "SELECT * FROM mytable ORDER BY count DESC 
LIMIT $first, $results_per_page";
?>

#3


0  

make a function passing four parameters
function getresult($index,$order,$start,$no_rows)
 {

    $sql="SELECT * FROM mytable order by $index $orders limit $start,$no_rows";
 }

where,

$index=on which sorting u want to do

$ index =你要做的排序

$order=asc/desc

$start=from on yow will start to get records

$ start = from on yow将开始获取记录

$no_$rows=you want to get no of rows

$ no_ $ rows =你想要没有行