prepare("SELECT * FROM users LIMIT :limit OFFSET :offset"); // Set limit and offset values $page = 2; $limit = 10; $offset = ($page - 1) * $limit; // Bind parameters and execute query $stmt->bindParam(':limit', $limit, PDO::PARAM_INT); $stmt->bindParam(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); // Fetch results and display $results = $stmt->fetchAll(); foreach ($results as $result) { echo $result['name'] . '
'; } ?>
createQueryBuilder(); // Set the query builder to query for User entities $qb->select('u') ->from('User', 'u') ->orderBy('u.id', 'ASC'); // Set the offset and limit for pagination $page = 2; $limit = 10; $offset = ($page - 1) * $limit; // Use setFirstResult and setMaxResults to apply offset and limit $qb->setFirstResult($offset) ->setMaxResults($limit); // Execute the query and fetch results $results = $qb->getQuery()->getResult(); // Display results foreach ($results as $result) { echo $result->getName() . 'This example uses Doctrine's setFirstResult function instead of setOffset, but serves the same purpose. The query builder object is used to generate a SQL query that selects User entities, orders them by their ID, and applies the offset and limit for pagination. Package/library: Doctrine ORM
'; } ?>