dpwtr666638 2016-01-26 16:44
浏览 12

如何使用next和previous获取mysql中的数据

I have a database which has more than 5,000 records, I would like to fetch 50 rows and then fetch the next 50 when user clicks next and the opposite when user click previous.

Table name: events

my trial was to

select * from events where entred_by = '$id' limit $limit, 50

but the problem is I am failing to calculate the next rows and previous rows.

edit

        $start = '0';
        $PAGE_P = $_GET['p'];
        $PAGE_N = $_GET['n'];
        if(!numeric($PAGE_N)){ 
            $PAGE_N = 50;
            $PAGE_P = 0;
            $start = '0';
        }else{
            if(isset($_GET['n'])){
                $PAGE_N = $PAGE_N + 50;
                $PAGE_P = $PAGE_N - 50;
                $start = $PAGE_N;
            }else{
                $PAGE_N = $PAGE_P + 50;
                $PAGE_P = $PAGE_N - 50; 
                $start = $PAGE_P;               
            }
        }
        $this->query = SQL::get("
            SELECT * FROM event 
            INNER JOIN user
            ON user.user_id = event.entered_by
            ORDER BY event_id ASC LIMIT $start,50
        ");

I have made an edit, with the edit I am able to use next button, which works, but when I press previous it is going back to row 1.

  • 写回答

1条回答 默认 最新

  • dqyat62284 2016-01-26 16:47
    关注
    SELECT * FROM events WHERE entred_by = '$id' ORDER BY id LIMIT $start, $limit
    

    Set the $start value (row number to start on) and $limit (50).

    You need to use the LIMIT keyword and probably ORDER BY too.

    Update for edit:

    Okay, ignoring the validation, it should be something like this:

    $this->query = SQL::get("
                SELECT * FROM event 
                INNER JOIN user
                ON user.user_id = event.entered_by
                ORDER BY event_id ASC 
                LIMIT $start_row, $number_of_rows
            ");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线