Liam Liam - 10 days ago 6
PHP Question

Pagination in PDO PHP

im trying to learn pagination with PHP/PDO.

$limit = 20;
$sth = $conn->prepare("SELECT * FROM directory WHERE user_active != ''");
$sth->execute(array(':county' => $county));

$c = 1;
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo 'Resutls will be here';
$c++;
}


Im unsure what to do next though, has anybody a good starting point I can reference from, or can explain the process for me?

Thanks

Answer

not tested

$page  = 1;
$limit = 20;
$start = $page * $limit;

$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$sth = $conn->prepare("SELECT * FROM directory WHERE user_active LIMIT ?,?");
$sth->execute(array($start,$limit));