Paginated method to get all items I'm using slim php and i have this simple paginated end point to get all clients, i feel like i could do some things here better, so i would appreciate all suggestions, if possible please give me code example with suggested improvements. Thank you in advance.

$app-&gt;get('/api/clients', function ($request, $response, $args) {
    require_once 'dbConnect.php';
    require_once 'shared/securityService.php';
    $queryParams = $request-&gt;getQueryParams();

    $total = getTotalCount($mysqli, $queryParams["group"]);
    $pageSize = $queryParams["pageSize"];

    $totalPages = ceil($total / $pageSize);
    $offset = ($queryParams["page"] - 1)  * $pageSize;

    if(is_numeric($queryParams["group"])) {
        $stmt = $mysqli-&gt;prepare("SELECT id, firstName, lastName, birthDate, phoneNumber, address,
            contactPerson, contactPersonPhoneNumber FROM client WHERE groupId = ? ORDER BY
            id LIMIT $pageSize OFFSET $offset");

        $stmt-&gt;bind_param("i", $queryParams["group"]);
    } else {
        $stmt = $mysqli-&gt;prepare("SELECT id, firstName, lastName, birthDate, phoneNumber, address,
            contactPerson, contactPersonPhoneNumber FROM client 
            ORDER BY id LIMIT $pageSize OFFSET $offset");            
    }

    $stmt-&gt;execute();
    $stmt-&gt;bind_result($id, $firstName, $lastName, $birthDate, $phoneNumber, 
        $address, $contactPerson, $contactPersonPhoneNumber);

    $data = null;

    while ($stmt-&gt;fetch()) {
        $data['items'][] = array(
            'id' =&gt; $id,
            'firstName' =&gt; $firstName,
            'lastName' =&gt; $lastName,
            'birthDate' =&gt; $birthDate,
            'phoneNumber' =&gt; $phoneNumber,
            'address' =&gt; $address,
            'contactPerson' =&gt; $contactPerson,
            'contactPersonPhoneNumber' =&gt; $contactPersonPhoneNumber
        );
    }

    $data['totalItems'] = $total;

    $stmt-&gt;close();
    $mysqli-&gt;close();
    echo json_encode($data);
});

function getTotalCount($mysqli, $groupId) {
    if(is_numeric($groupId)) {
        $totalStmt = $mysqli-&gt;prepare("SELECT COUNT(id) FROM client WHERE groupId = ?");
        $totalStmt-&gt;bind_param("i", $groupId);
    } else {
        $totalStmt = $mysqli-&gt;prepare("SELECT COUNT(id) FROM client");
    }

    $totalStmt-&gt;execute();
    $totalStmt-&gt;bind_result($total);
    $totalStmt-&gt;fetch();
    $totalStmt-&gt;close();
    return $total;
}
SQL Injection

You are vulnerable to SQL injection via pageSize. 

You need to use prepared statements for all values, not just some of them. 

Naming

total and getTotalCount could be more descriptive. Currently, I don't really know what "total" means without looking at the code of the function. You also don't need "total" at all (it's assumed when talking about a count). $clientCount = getClientCountForGroup($mysqli, $id) would be easier to understand. 

Duplication

Your two queries are basically duplicates apart from the WHERE clause. You may want to remove that duplication.