简体   繁体   中英

PHP and MySQL pagination issue

I am working on a page to fetch photo files from a database, and I'm trying to paginate the results. However, when I do so I realize that LIMIT is returning the first page, and only the first page. My code before the query seems to be fine. I don't know what's happening and I need some help. Here is my code:

<?php
require "../includes/db/config.php";
require "../includes/db/db.php";

$page=(!empty($_REQUEST['page']))?(int)$_REQUEST['page']:1;
$perPage=isset($_GET['perpage'])?(int)$_GET['perpage']:3;

$start=($page>1)?($page*$perPage)-$perPage:0;
$photos=$conn->prepare("
    SELECT * 
    FROM cars 
    LIMIT {$perPage}
");

$photos->execute();

$photos=$photos->fetchAll(PDO::FETCH_ASSOC);
var_dump($photos);
?>

I have checked everywhere to see what the problem is and found naught. When I var_dump I realize it returns just the three that I request, which is correct. But it doesn't change when I put in the URL 'page=2', ie, it returns the same values as page 1. Does anyone know why this is so?

OFFSET. Offset is really important. It would've always started from the first value because I didn't set it to always look at what value to start at.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM