douxiuyu2028 2013-06-30 12:28
浏览 10
已采纳

PHP / MySQL SELECT LIMITER问题

I posted this question before but I did not know the cause but now I do. The other question is way out of hand.

I am making a page function (next page etc..) and need my $limiter to select 9 querys per page. The issue I am having is that the second page (9,9) does not come in.

if(isset($_GET["p"]) && is_numeric($_GET["p"]) && $_GET["p"] > 1) {
        $currentPage = $_GET["p"];
        $limiter = $currentPage * 9;
} else {
        $currentPage = 1;
        $limiter = 0;
}

$finalQuery = "SELECT * FROM forum_replies WHERE thread_id = '1' ORDER BY id ASC LIMIT " . $limiter . ",9";

So if page is 1, $limiter is 0. So.. 0,9 comes in

Next should be 9,9 but the code completley skips that so I am missing out on page two

Page three works which is 18,9

So.. it goes 0,9 - 18,9, 27,9 etc.. completely skipping 9,9. What would I add to my if(isset)) function to make it select all if it and not skip 9,9.

  • 写回答

3条回答 默认 最新

  • dta38159 2013-06-30 12:41
    关注

    If i am not wrong i think your calculations for $limiter variable is incorrect. it should be $limiter = ($currentPage - 1) * 9;

    if(isset($_GET["p"]) && is_numeric($_GET["p"]) && $_GET["p"] > 1) {
        $currentPage = $_GET["p"];
        $limiter = ($currentPage -1) * 9;
    } else {
        $currentPage = 1;
        $limiter = 0;
    }
    
    $finalQuery = "SELECT * FROM forum_replies WHERE thread_id = '1' ORDER BY id ASC LIMIT " . $limiter . ",9";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵