Query a MySQL Db using PHP and PDO with paginated results.
<?php
namespace Entities;
//Fields/Properties must match column names in database
class MyObject
{
public $Id = 0;
public $Title;
public $UserId;
.....
}
.....
function List_By_Page($page, $resultsPerPage)
{
$sql = "select TABLENAME.* from TABLENAME";
//WHERE Clause
//$sql = $sql . " where TABLENAME.FieldToFilter = :fieldToFilter";
if ($resultsPerPage > 0)
{
if ($page < 1)
{
$page = 1;
}
$limitStart = ($page * $resultsPerPage) - $resultsPerPage;
$sql = $sql . " limit " . $limitStart . ", " . $resultsPerPage;
}
$query = $this->db->prepare($sql);
//bind values
//$query->bindValue(":fieldToFilter", $valueToFilterBy);
$query->execute();
return $query->fetchALL(\PDO::FETCH_CLASS, "Entities\MyObject");
}