How to paginate mysql database results using php function

April 11, 2020

We want a php functiont that will return the rows of a database table as an associative array. Optionaly the function gets a page number and the rows-per-page number so we can limit the results.


// return an array with
function getList($table, $page=1, $pageLength=24) {
    global $mysqli;
    $rows = [];

    $start = ($page-1)*$pageLength;
    $res = $mysqli->query("SELECT * FROM $table LIMIT $start, $pageLength");
    while($row = $res->fetch_assoc()) {
        $rows[] = $row;
    return $rows;