gpt4 book ai didi

php - 如果您的 mysql 查询中有某些条件(Where 子句),如何创建分页?

转载 作者:行者123 更新时间:2023-11-30 23:04:50 26 4
gpt4 key购买 nike

我有这样的查询:

     $pagenumber = get_page_number(); // I'll get the pagenumber by a method wich runs when user click's on page numbers , for example : 2 or 3 or whatevere user clicks.


//Now , I'll make A Limit caluse :

$limit = $pagenumber.",10"; // limit the page to show 10 result => example : LIMIT 2,10

$sql = "SELECT * From doctors LIMIT $limit";
--> after this , I show the result which is by my on way (echo a div for every result );

这是一个很酷的分页,工作正常。


但问题是,当我的查询中有一个 WHERE 时,mysql 变得困惑,我不知道该怎么做

考虑一下:

     $sql = "SELECT * FROM doctors WHERE `firstname` LIKE '$firstname' LIMIT $limit";    

所以,它不起作用:((

我知道问题出在“WHERE”子句上,因为当用户点击页码时,我的$limit 将是例如:2,10 我的查询是这样的:

    $sql = "SELECT * FROM doctors Where firstname LIKE '$firstname' LIMIT 2,10";    

这里有一个条件(WHERE firstname ....)mysql 怎么知道从哪里开始搜索?(通过起点,我的意思是如果“LIMIT x,y”x 是起点)

mysql 如何知道“LIMIT 2,10”是什么意思。

placed 2 在我的表中的什么位置?(当有条件时);

如果没有条件,2表示第21条记录(我说的对吗?),当然如果offset是10; (限制 2,10) ;

是这样吗?

但是,如果有一个 WHERE 子句呢?????

现在起点是什么?(2????)

如果您想要更具体的细节,请随时询问(我想我已经解释得很清楚了)谢谢

最佳答案

$pagesize = 10;
$start = ($page) * $pagesize; // assuming you're passing in 0 based pages, if they're 1 base, do a $page -1

$sql = "SELECT * FROM doctors Where firstname LIKE '$firstname' ORDER BY lastname LIMIT $start,$pagesize";

您需要对结果集进行排序以使限制有意义。此外,开始需要考虑页面大小。

关于php - 如果您的 mysql 查询中有某些条件(Where 子句),如何创建分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22330390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com