如何使用下一个和上一个在 mysql 中获取数据


How to fetch data in mysql with next and previous

我有一个数据库,其中有超过 5,000 条记录,我想获取 50 行,然后在用户单击下一个时获取下一个 50 行,当用户单击上一个时获取相反的行。

表名:事件

我的审判是

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

但问题是我无法计算下一行和前几行。

编辑

        $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
        ");

我已经进行了编辑,通过编辑,我可以使用下一步按钮,该按钮有效,但是当我按上一次时,它会返回到第 1 行。

SELECT * FROM events WHERE entred_by = '$id' ORDER BY id LIMIT $start, $limit

设置$start值(要从其开始的行号)和$limit (50)。

您需要使用 LIMIT 关键字,可能还需要使用 ORDER BY。

更新编辑:

好的,忽略验证,它应该是这样的:

$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
        ");